Subversion Repositories HomeAutomation

Rev

Rev 986 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 986 Rev 995
Line 91... Line 91...
91
 
91
 
92
 
92
 
93
    //access global context within this scope
93
    //access global context within this scope
94
    Context::Scope context_scope(myContext);
94
    Context::Scope context_scope(myContext);
95
    //exception handler
95
    //exception handler
96
    TryCatch try_catch;
96
    TryCatch tryCatch;
97
    //compile script to binary code - JIT
97
    //compile script to binary code - JIT
98
    Handle<Script> script = Script::Compile(source, name);
98
    Handle<Script> script = Script::Compile(source, name);
99
 
99
 
100
    //check if we got problems on compilation
100
    //check if we got problems on compilation
101
    if (script.IsEmpty())
101
    if (script.IsEmpty())
102
    {
102
    {
103
 
-
 
104
        // Print errors that happened during compilation.
-
 
105
        String::AsciiValue error(try_catch.Exception());
103
        printException(&tryCatch);
106
        string sError = *error;
104
        slog << "\n";
107
        slog << "Failed to compile script: " + sError + "\n";
105
        return;
108
    }
106
    }
109
    else
107
    else
110
    {
108
    {
111
        //no errors , let's continue
109
        //no errors , let's continue
112
        Handle<Value> result = script->Run();
110
        Handle<Value> result = script->Run();
113
 
111
 
114
        //check if execution ended with errors
112
        //check if execution ended with errors
115
        if (result.IsEmpty())
113
        if (result.IsEmpty())
116
        {
114
        {
117
            // Print errors that happened during execution.
-
 
118
            String::AsciiValue error(try_catch.Exception());
115
            printException(&tryCatch);
119
            string sError = *error;
116
            slog << "\n";
120
            slog << "Constructor: Failed to run script: " << sError << "\n";
117
            return;
121
        }
118
        }
122
        else
119
        else
123
        {
120
        {
124
            loadScript("System/Startup.js");
121
            loadScript("System/Startup.js");
125
 
122
 
Line 164... Line 161...
164
                callHandleNMTMessage(canMessage);
161
                callHandleNMTMessage(canMessage);
165
            }
162
            }
166
            else
163
            else
167
            {
164
            {
168
                callHandleMessage(canMessage);
165
                callHandleMessage(canMessage);
169
            }
166
            }
170
        }
167
        }
171
 
168
 
172
        mySemaphore.wait();
169
        mySemaphore.wait();
173
    }
170
    }
174
 
171
 
175
    mySemaphore.unlock();
172
    mySemaphore.unlock();
176
}
173
}
177
 
174
 
178
void VirtualMachine::queueCanMessage(CanMessage canMessage)
175
void VirtualMachine::queueCanMessage(CanMessage canMessage)
179
{
176
{
180
    myCanMessages.push(canMessage);
177
    myCanMessages.push(canMessage);
181
    mySemaphore.broadcast();
178
    mySemaphore.broadcast();
182
}
179
}
183
 
180
 
184
void VirtualMachine::queueExpression(string expression)
181
void VirtualMachine::queueExpression(string expression)
185
{
182
{
186
    myExpressions.push(expression);
183
    myExpressions.push(expression);
187
    mySemaphore.broadcast();
184
    mySemaphore.broadcast();
188
}
185
}
189
 
186
 
190
bool VirtualMachine::loadScript(string scriptName)
187
bool VirtualMachine::loadScript(string scriptName)
191
{
188
{
192
    SyslogStream &slog = SyslogStream::getInstance();
189
    SyslogStream &slog = SyslogStream::getInstance();
193
 
190
 
194
    string basePath = Settings::get("BasePath") + "Services/";
191
    string basePath = Settings::get("BasePath") + "Services/";
195
    string scriptFileName = basePath + scriptName;
192
    string scriptFileName = basePath + scriptName;
196
 
193
 
197
    if (!file_exists(scriptFileName))
194
    if (!file_exists(scriptFileName))
198
    {
195
    {
199
        slog << "Failed to load " + scriptFileName + "\n";
196
        slog << "Failed to load " + scriptFileName + "\n";
200
        return false;
197
        return false;
201
    }
198
    }
202
 
-
 
203
    string scriptSource = file_get_contents(scriptFileName);
-
 
204
 
-
 
205
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
-
 
206
    //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed
-
 
207
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
-
 
208
 
-
 
209
    //access global context within this scope
-
 
210
    Context::Scope context_scope(myContext);
-
 
211
    //exception handler
-
 
212
    TryCatch try_catch;
-
 
213
    //compile script to binary code - JIT
-
 
214
    Handle<Script> script = Script::Compile(source, name);
-
 
215
 
-
 
216
    //check if we got problems on compilation
-
 
217
    if (script.IsEmpty())
-
 
218
    {
-
 
219
        // Print errors that happened during compilation.
-
 
220
        String::AsciiValue error(try_catch.Exception());
-
 
221
        string sError = *error;
-
 
222
        slog << "loadScript: Failed to compile script: " + sError + "\n";
-
 
223
        return false;
-
 
224
    }
-
 
225
    else
-
 
226
    {
-
 
227
        //no errors , let's continue
-
 
228
        Handle<Value> result = script->Run();
-
 
229
 
-
 
230
        //check if execution ended with errors
-
 
231
        if (result.IsEmpty())
-
 
232
        {
-
 
233
            // Print errors that happened during execution.
-
 
234
            String::AsciiValue error(try_catch.Exception());
-
 
235
            string sError = *error;
-
 
236
            slog << "loadScript: Failed to run script: " + sError + "\n";
-
 
237
            return false;
-
 
238
        }
-
 
239
 
199
 
-
 
200
    string scriptSource = file_get_contents(scriptFileName);
-
 
201
 
-
 
202
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
-
 
203
    //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed
-
 
204
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
-
 
205
 
-
 
206
    //access global context within this scope
-
 
207
    Context::Scope context_scope(myContext);
-
 
208
    //exception handler
-
 
209
    TryCatch tryCatch;
-
 
210
    //compile script to binary code - JIT
-
 
211
    Handle<Script> script = Script::Compile(source, name);
-
 
212
 
-
 
213
    //check if we got problems on compilation
-
 
214
    if (script.IsEmpty())
-
 
215
    {
-
 
216
        printException(&tryCatch);
-
 
217
        slog << "\n";
-
 
218
        return false;
-
 
219
    }
-
 
220
    else
-
 
221
    {
-
 
222
        //no errors , let's continue
-
 
223
        Handle<Value> result = script->Run();
-
 
224
 
-
 
225
        //check if execution ended with errors
-
 
226
        if (result.IsEmpty())
-
 
227
        {
-
 
228
            printException(&tryCatch);
-
 
229
            slog << "\n";
-
 
230
            return false;
-
 
231
        }
-
 
232
 
240
        slog << "Loaded " + scriptName + "\n";
233
        slog << "Loaded " + scriptName + "\n";
241
    }
234
    }
242
 
235
 
243
    return true;
236
    return true;
244
}
237
}
245
 
238
 
246
void VirtualMachine::callHandleNMTMessage(CanMessage canMessage)
239
void VirtualMachine::callHandleNMTMessage(CanMessage canMessage)
Line 325... Line 318...
325
        mySocketThreads[id]->send(data);
318
        mySocketThreads[id]->send(data);
326
    }
319
    }
327
}
320
}
328
 
321
 
329
bool VirtualMachine::runExpression(string expression)
322
bool VirtualMachine::runExpression(string expression)
330
{
323
{
331
    SyslogStream &slog = SyslogStream::getInstance();
324
    SyslogStream &slog = SyslogStream::getInstance();
332
 
325
 
333
    string scriptName = "runExpression";
326
    string scriptName = "runExpression";
334
 
327
 
335
    Handle<String> source =  String::New(expression.c_str(), expression.length());
328
    Handle<String> source =  String::New(expression.c_str(), expression.length());
336
    //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed
329
    //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed
337
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
330
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
338
 
331
 
339
    //access global context within this scope
332
    //access global context within this scope
340
    Context::Scope context_scope(myContext);
333
    Context::Scope context_scope(myContext);
341
    //exception handler
334
    //exception handler
342
    TryCatch try_catch;
335
    TryCatch tryCatch;
343
    //compile script to binary code - JIT
336
    //compile script to binary code - JIT
344
    Handle<Script> script = Script::Compile(source, name);
337
    Handle<Script> script = Script::Compile(source, name);
345
 
338
 
346
    //check if we got problems on compilation
339
    //check if we got problems on compilation
347
    if (script.IsEmpty())
340
    if (script.IsEmpty())
348
    {
341
    {
349
        // Print errors that happened during compilation.
-
 
350
        String::AsciiValue error(try_catch.Exception());
-
 
351
        string sError = *error;
342
        printException(&tryCatch);
352
        slog << "runExpression: Failed to compile script: " + sError + "\n";
-
 
353
        slog << expression << "\n\n";
343
        slog << "\n";
354
        return false;
344
        return false;
355
    }
345
    }
356
    else
346
    else
357
    {
347
    {
358
        //no errors , let's continue
348
        //no errors , let's continue
359
        Handle<Value> result = script->Run();
349
        Handle<Value> result = script->Run();
360
 
350
 
361
        //check if execution ended with errors
351
        //check if execution ended with errors
362
        if (result.IsEmpty())
352
        if (result.IsEmpty())
363
        {
353
        {
364
            // Print errors that happened during execution.
-
 
365
            String::AsciiValue error(try_catch.Exception());
354
            printException(&tryCatch);
366
            string sError = *error;
355
            slog << "\n";
367
            slog << "runExpression: Failed to run script: " + sError + "\n";
-
 
368
            return false;
356
            return false;
369
        }
357
        }
370
    }
358
    }
371
 
359
 
372
    return true;
360
    return true;
373
}
361
}
-
 
362
 
-
 
363
void VirtualMachine::printException(TryCatch* tryCatch)
-
 
364
{
-
 
365
    SyslogStream &slog = SyslogStream::getInstance();
-
 
366
 
-
 
367
    HandleScope handle_scope;
-
 
368
    String::Utf8Value exception(tryCatch->Exception());
-
 
369
    Handle<v8::Message> message = tryCatch->Message();
-
 
370
 
-
 
371
    string strException = *exception;
-
 
372
 
-
 
373
    if (message.IsEmpty())
-
 
374
    {
-
 
375
        // V8 didn't provide any extra information about this error; just print the exception.
-
 
376
        slog << strException + "\n";
-
 
377
    }
-
 
378
    else
-
 
379
    {
-
 
380
        // Print (filename):(line number): (message).
-
 
381
        String::Utf8Value filename(message->GetScriptResourceName());
-
 
382
        int linenum = message->GetLineNumber();
-
 
383
 
-
 
384
        string strFilename = *filename;
-
 
385
       
-
 
386
        slog << strFilename + ":" + itos(linenum) + ": " + strException + "\n";
-
 
387
 
-
 
388
        // Print line of source code.
-
 
389
        String::Utf8Value sourceline(message->GetSourceLine());
-
 
390
 
-
 
391
        string strSourceline = *sourceline;
-
 
392
 
-
 
393
        slog << strSourceline + "\n";
-
 
394
 
-
 
395
        string underline;
-
 
396
        // Print wavy underline (GetUnderline is deprecated).
-
 
397
        int start = message->GetStartColumn();
-
 
398
        for (int i = 0; i < start; i++)
-
 
399
        {
-
 
400
            underline += " ";
-
 
401
        }
-
 
402
 
-
 
403
        int end = message->GetEndColumn();
-
 
404
        for (int i = start; i < end; i++)
-
 
405
        {
-
 
406
            underline += "^";
-
 
407
        }
-
 
408
 
-
 
409
        slog << underline + "\n";
-
 
410
    }
-
 
411
}
-
 
412
 
374
 
413
 
375
Handle<Value> VirtualMachine_log(const Arguments& args)
414
Handle<Value> VirtualMachine_log(const Arguments& args)
376
{
415
{
377
    SyslogStream &slog = SyslogStream::getInstance();
416
    SyslogStream &slog = SyslogStream::getInstance();
378
 
417