/* udeb_print() is used by macros in the public API */ void udeb_print(const char *src_path, int level, const char *fmt, ...) { int sz = 0; char message[UDEB_MSG_BUF]; const char * src_file = udeb_basename(src_path); /* Construct the message */ va_list args; va_start(args, fmt); sz += snprintf(message, UDEB_MSG_BUF, "%s ", src_file); sz += vsnprintf(message + sz, UDEB_MSG_BUF - sz, fmt, args); va_end(args); if(udeb_initialized && log_level(src_file) >= level) { #if SEND_MESSAGES_TO_JAVASCRIPT HandleScope scope; Handle<Value> jsArgs[3]; jsArgs[0] = Number::New(level); jsArgs[1] = String::New(src_file); jsArgs[2] = String::New(message, sz); JSLoggerFunction->Call(Context::GetCurrent()->Global(), 3, jsArgs); #else sprintf(message + sz, "\n"); fputs(message, stderr); #endif } return; }
/* * Class: org_appcelerator_kroll_runtime_v8_V8Runtime * Method: nativeRunModule * Signature: (Ljava/lang/String;Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeRunModule (JNIEnv *env, jobject self, jstring source, jstring filename, jobject activityProxy) { ENTER_V8(V8Runtime::globalContext); titanium::JNIScope jniScope(env); if (moduleObject.IsEmpty()) { moduleObject = Persistent<Object>::New( V8Runtime::krollGlobalObject->Get(String::New("Module"))->ToObject()); runModuleFunction = Persistent<Function>::New( Handle<Function>::Cast(moduleObject->Get(String::New("runModule")))); } Handle<Value> jsSource = TypeConverter::javaStringToJsString(source); Handle<Value> jsFilename = TypeConverter::javaStringToJsString(filename); Handle<Value> jsActivity = TypeConverter::javaObjectToJsValue(activityProxy); Handle<Value> args[] = { jsSource, jsFilename, jsActivity }; TryCatch tryCatch; runModuleFunction->Call(moduleObject, 3, args); if (tryCatch.HasCaught()) { V8Util::openJSErrorDialog(tryCatch); V8Util::reportException(tryCatch, true); } }
bool V8Util::isNaN(Handle<Value> value) { HandleScope scope; Local<Object> global = Context::GetCurrent()->Global(); if (isNaNFunction.IsEmpty()) { Local<Value> isNaNValue = global->Get(String::NewSymbol("isNaN")); isNaNFunction = Persistent<Function>::New(isNaNValue.As<Function> ()); } Handle<Value> args[] = { value }; return isNaNFunction->Call(global, 1, args)->BooleanValue(); }
Handle<Value> ODBC::CallbackSQLError (SQLSMALLINT handleType, SQLHANDLE handle, char* message, Persistent<Function> cb) { HandleScope scope; Local<Object> objError = ODBC::GetSQLError( handleType, handle, message ); Local<Value> args[1]; args[0] = objError; cb->Call(Context::GetCurrent()->Global(), 1, args); return scope.Close(Undefined()); }
int main(int argc, char** argv) { signal(SIGSEGV, AnsiSignalHandler); // printf("SILK V0.1\n"); if (argc < 2) { printf("usage: %s file.js\n", argv[0]); exit(1); } char *startup = readFile(argv[1]); if (!startup) { printf("%s not found\n", argv[1]); exit(1); } if (startup[0] == '#' && startup[1] == '!') { startup[0] = startup[1] = '/'; } init_global_object(); { HandleScope scope; context = Persistent<Context>::New(Context::New(NULL, globalObject)); Context::Scope context_scope(context); // Debug::EnableAgent("silkjs", 9222); // Debug::SetDebugMessageDispatchHandler(debugger, true); Handle<Script>init = Script::New(String::New("global=this;"), String::New("builtin")); init->Run(); mainScript = Persistent<Script>::New(Script::Compile(String::New(startup), String::New(argv[1]))); mainScript->Run(); Handle<String> process_name = String::New("main"); Handle<Value> process_val = context->Global()->Get(process_name); Handle<Function> process_fun = Handle<Function>::Cast(process_val); mainFunc = Persistent<Function>::New(process_fun); const int ac = argc-2; Handle<Value>av[ac]; for (int i=2; i<argc; i++) { av[i-2] = String::New(argv[i]); } // printf("SILKJS running %s\n", argv[1]); mainFunc->Call(context->Global(), ac, av); } }
void HandleEvent (EventHandler* handler) { if (handler->f->IsFunction()) { Persistent<Function> f = Persistent<Function>::Cast<Value>(handler->f); Handle<Value> args[3]; args[0] = handler->event; args[1] = Int32::New(handler->type); args[2] = Local<Value>::New(handler->data); f->Call(f, 3, args); f.Clear(); } handler->f.Dispose(); handler->f.Clear(); handler->data.Dispose(); handler->data.Clear(); handler->event.Dispose(); handler->event.Clear(); delete handler; }
JNIEXPORT void JNICALL Java_ag_boersego_bgjs_ClientAndroid_runCBBoolean (JNIEnv * env, jobject obj, jlong ctxPtr, jlong cbPtr, jlong thisPtr, jboolean b) { BGJSContext* context = (BGJSContext*)ctxPtr; v8::Locker l; Context::Scope context_scope(context->_context); HandleScope scope; TryCatch trycatch; Persistent<Function> fn = static_cast<Function*>((Function*)cbPtr); Persistent<Object> thisObj = static_cast<Object*>((void*)thisPtr); LOGD("runOn %llu %llu", cbPtr, thisPtr); int argcount = 1; Handle<Value> argarray[] = { Boolean::New(b ? true : false)}; Handle<Value> result = fn->Call(thisObj, argcount, argarray); if (result.IsEmpty()) { BGJSContext::ReportException(&trycatch); } }
JNIEXPORT void JNICALL Java_ag_boersego_bgjs_ClientAndroid_timeoutCB( JNIEnv * env, jobject obj, jlong ctxPtr, jlong jsCbPtr, jlong thisPtr, jboolean cleanup, jboolean runCb) { BGJSContext* context = (BGJSContext*)ctxPtr; v8::Locker l; Context::Scope context_scope(context->_context); HandleScope scope; TryCatch trycatch; // Persistent<Function>* callbackPers = (Persistent<Function>*) jsCbPtr; WrapPersistentObj* wo = (WrapPersistentObj*)thisPtr; Persistent<Object> thisObj = wo->obj; WrapPersistentFunc* ws = (WrapPersistentFunc*)jsCbPtr; Persistent<Function> callbackP = ws->callbackFunc; if (DEBUG) { LOGI("timeoutCb called, cbPtr is %llu, thisObj %llu, ctxPtr %llu", jsCbPtr, thisPtr, ctxPtr); } if (runCb) { int argcount = 0; Handle<Value> argarray[] = { }; Handle<Value> result = callbackP->Call(thisObj, argcount, argarray); if (result.IsEmpty()) { BGJSContext::ReportException(&trycatch); } } if (cleanup) { if (DEBUG) { LOGI("timeoutCb cleaning up"); } thisObj.Dispose(); delete(wo); callbackP.Dispose(); delete(ws); } }
int NovaNode::HandleMessageWithIDOnV8Thread(eio_req *arg) { Nova::Message *message = static_cast<Nova::Message*>(arg->data); if(jsCallbacks.count(message->m_contents.m_messageid()) > 0) { HandleScope scope; Persistent<Function> function = jsCallbacks[message->m_contents.m_messageid()]; Local<Value> argv[1]; if(message->m_contents.m_success()) { argv[0] = {Local<Value>::New(String::New((message->m_suspects[0]->ToString() + message->m_suspects[0]->m_features.toString()).c_str()))}; } else { argv[0] = {Local<Value>::New(String::New(""))}; } function->Call(Context::GetCurrent()->Global(), 1, argv); jsCallbacks.erase(message->m_contents.m_messageid()); } return 0; }
int main(int argc, char* argv[]) { HandleScope handleScope; Local<FunctionTemplate> logFunction = FunctionTemplate::New(LogCallback); Local<ObjectTemplate> globals = ObjectTemplate::New(); globals->Set(String::New("Log"),logFunction); Handle<Context> context = Context::New(NULL,globals); g_context = Persistent<Context>::New(context); // make the context global Context::Scope scope(g_context); Persistent<Function> updateFunction = GetFunctionHandle("test.js","Update"); printf("\n\n... Running Code ...\n\n"); const int numArgs=0; Handle<Value> * args = NULL; Handle<Value> result = updateFunction->Call( g_context->Global(), numArgs, args); // Convert the result to an ASCII string and print it. String::AsciiValue ascii(result); printf("The Result is %s\n", *ascii); return 0; }
Handle<Value> WrappedScript::EvalMachine(const Arguments& args) { HandleScope scope; if (input_flag == compileCode && args.Length() < 1) { return ThrowException(Exception::TypeError(String::New("needs at least 'code' argument."))); } const int sandbox_index = input_flag == compileCode ? 1 : 0; if (context_flag == userContext && args.Length() < (sandbox_index + 1)) { return ThrowException(Exception::TypeError(String::New("needs a 'context' argument."))); } Local<String> code; if (input_flag == compileCode) code = args[0]->ToString(); Local<Object> sandbox; if (context_flag == newContext) { sandbox = args[sandbox_index]->IsObject() ? args[sandbox_index]->ToObject() : Object::New(); } else if (context_flag == userContext) { sandbox = args[sandbox_index]->ToObject(); } const int filename_index = sandbox_index + (context_flag == newContext ? 1 : 0); Local<String> filename = args.Length() > filename_index ? args[filename_index]->ToString() : String::New("evalmachine.<anonymous>"); const int display_error_index = args.Length() - 1; bool display_error = false; if (args.Length() > display_error_index && args[display_error_index]->IsBoolean() && args[display_error_index]->BooleanValue() == true) { display_error = true; } Persistent<Context> context; Local<Array> keys; unsigned int i; WrappedContext *nContext = NULL; Local<Object> contextArg; if (context_flag == newContext) { // Create the new context context = Context::New(); } else if (context_flag == userContext) { // Use the passed in context contextArg = args[sandbox_index]->ToObject(); nContext = NativeObject::Unwrap<WrappedContext>(contextArg); context = nContext->GetV8Context(); } // New and user context share code. DRY it up. if (context_flag == userContext || context_flag == newContext) { // Enter the context context->Enter(); // Call the initCallback, if it exists if (nContext) { Persistent<Function> initCallback = nContext->GetInitCallback(); if (!initCallback.IsEmpty()) { Handle<Value> callbackArgs[] = { contextArg, context->Global() }; initCallback->Call(contextArg, 2, callbackArgs); } } // Copy everything from the passed in sandbox (either the persistent // context for runInContext(), or the sandbox arg to runInNewContext()). keys = sandbox->GetPropertyNames(); for (i = 0; i < keys->Length(); i++) { Handle<String> key = keys->Get(Integer::New(i))->ToString(); Handle<Value> value = sandbox->Get(key); if (value == sandbox) { value = context->Global(); } context->Global()->Set(key, value); } } Handle<Value> result; Handle<Script> script; if (input_flag == compileCode) { // well, here WrappedScript::New would suffice in all cases, but maybe // Compile has a little better performance where possible script = output_flag == returnResult ? Script::Compile(code, filename) : Script::New(code, filename); if (script.IsEmpty()) { // Hack because I can't get a proper stacktrace on SyntaxError return Undefined(); } } else { WrappedScript *n_script = NativeObject::Unwrap<WrappedScript>(args.Holder()); if (!n_script) { return ThrowException(Exception::Error(String::New("Must be called as a method of Script."))); } else if (n_script->script_.IsEmpty()) { return ThrowException(Exception::Error(String::New("'this' must be a result of previous " "new Script(code) call."))); } script = n_script->script_; } if (output_flag == returnResult) { result = script->Run(); if (result.IsEmpty()) { if (context_flag == newContext) { context->DetachGlobal(); context->Exit(); context.Dispose(); } return Undefined(); } } else { WrappedScript *n_script = NativeObject::Unwrap<WrappedScript>(args.Holder()); if (!n_script) { return ThrowException(Exception::Error(String::New("Must be called as a method of Script."))); } n_script->script_ = Persistent<Script>::New(script); result = args.This(); } if (context_flag == userContext || context_flag == newContext) { // success! copy changes back onto the sandbox object. keys = context->Global()->GetPropertyNames(); for (i = 0; i < keys->Length(); i++) { Handle<String> key = keys->Get(Integer::New(i))->ToString(); Handle<Value> value = context->Global()->Get(key); if (value == context->Global()) { value = sandbox; } sandbox->Set(key, value); } } if (context_flag == newContext) { // Clean up, clean up, everybody everywhere! context->DetachGlobal(); context->Exit(); context.Dispose(); } else if (context_flag == userContext) { // Exit the passed in context. context->Exit(); } if (result->IsObject()) { Local<Context> creation = result->ToObject()->CreationContext(); } return result == args.This() ? result : scope.Close(result); }
int main (int argc, char** argv) { signal(SIGSEGV, AnsiSignalHandler); // printf("SILK V0.1\n"); char *startup; const char *progName; signal(SIGSEGV, AnsiSignalHandler); signal(SIGINT, AnsiSignalHandler); if (argc < 2) { startup = readFile("/usr/local/silkjs/builtin/interpreter.js"); if (!startup) { startup = readFile("/usr/share/silkjs/builtin/interpreter.js"); } progName = "interpreter"; } else { startup = readFile(argv[1]); progName = argv[1]; } if (!startup) { printf("%s not found\n", argv[1]); exit(1); } if (startup[0] == '#' && startup[1] == '!') { startup[0] = startup[1] = '/'; } // v8 command line switches const char *switches = "--harmony"; V8::SetFlagsFromString(switches, strlen(switches)); { // Isolate *isolate = Isolate::New(); // isolate->Enter(); // Locker lock(isolate); Locker locker; HandleScope scope; init_global_object(); V8::SetCaptureStackTraceForUncaughtExceptions(true, 50); // , StackTrace::kDetailed); context = Context::New(NULL, globalObject); Context::Scope context_scope(context); { Locker locker; // Debug::SetDebugMessageDispatchHandler(debugger, true); // Debug::EnableAgent("silkjs", 5858); Handle<Script>init = Script::New(String::New("global=this; module = {}; include('builtin/all.js');"), String::New("builtin")); init->Run(); V8::SetCaptureStackTraceForUncaughtExceptions(true, 50, StackTrace::kDetailed); TryCatch tryCatch; mainScript = Persistent<Script>::New(Script::Compile(String::New(startup), String::New(progName))); if (mainScript.IsEmpty()) { ReportException(&tryCatch); exit(1); } Handle<Value>v = mainScript->Run(); if (v.IsEmpty()) { ReportException(&tryCatch); exit(1); } Handle<String> process_name = String::New("main"); Handle<Value> process_val = context->Global()->Get(process_name); if (!process_val.IsEmpty() && process_val->IsFunction()) { Handle<Function> process_fun = Handle<Function>::Cast(process_val); mainFunc = Persistent<Function>::New(process_fun); int ac = argc - 2; if (ac < 0) { ac = 0; } Handle<Value>av[ac]; for (int i = 2; i < argc; i++) { av[i - 2] = String::New(argv[i]); } v = mainFunc->Call(context->Global(), ac, av); if (v.IsEmpty()) { ReportException(&tryCatch); exit(1); } } } context.Dispose(); } }
JNIEXPORT bool JNICALL Java_ag_boersego_bgjs_ClientAndroid_ajaxDone( JNIEnv * env, jobject obj, jlong ctxPtr, jstring dataStr, jint responseCode, jlong jsCbPtr, jlong thisPtr, jlong errorCb, jboolean success, jboolean processData) { BGJSContext* context = (BGJSContext*)ctxPtr; const char *nativeString = env->GetStringUTFChars(dataStr, 0); v8::Locker l; Context::Scope context_scope(context->_context); HandleScope scope; TryCatch trycatch; Persistent<Object> thisObj = static_cast<Object*>((void*)thisPtr); Persistent<Function> errorP; if (errorCb) { errorP = static_cast<Function*>((void*)errorCb); } Persistent<Function> callbackP = static_cast<Function*>((void*)jsCbPtr); Handle<Value> argarray[1]; int argcount = 1; if (nativeString == 0 || dataStr == 0) { argarray[0] = v8::Null(); } else { Handle<Value> resultObj; if (processData) { resultObj = context->JsonParse(thisObj, String::New(nativeString)); } else { resultObj = String::New(nativeString); } argarray[0] = resultObj; } Handle<Value> result; if (success) { result = callbackP->Call(thisObj, argcount, argarray); } else { if (!errorP.IsEmpty()) { result = errorP->Call(thisObj, argcount, argarray); } else { LOGI("Error signaled by java code but no error callback set"); result = v8::Undefined(); } } if (result.IsEmpty()) { BGJSContext::ReportException(&trycatch); } env->ReleaseStringUTFChars(dataStr, nativeString); callbackP.Dispose(); thisObj.Dispose(); if (!errorP.IsEmpty()) { errorP.Dispose(); } return true; }