static void gulong gom_signal_connect_script (JSContext *cx, JSObject *obj, const char *signal, const char *script, gsize scriptlen, GError **error) { SignalData *data = NULL; GObject *gobj; guint signal_id; GCallback c_handler; gobj = gom_js_object_get_g_object (cx, obj); if (!gobj) { g_printerr ("Could not find GObject for JSObject %p\n", obj); return 0; } signal_id = g_signal_lookup (signal, G_TYPE_FROM_INSTANCE (gobj)); if (!signal_id) { g_printerr ("Could not find signal %s for %s %p\n", signal, g_type_name (G_TYPE_FROM_INSTANCE (gobj)), gobj); return; } data = g_new0 (SignalData, 1); data->node = g_hash_table_lookup (signals, GINT_TO_POINTER (signal_id)); if (!data->node) { g_printerr ("Could not find signal node for signal %s on %s %p\n", signal, g_type_name (G_TYPE_FROM_INSTANCE (gobj)), gobj); goto script_fail; } data->script = JS_CompileScript (cx, obj, script, scriptlen, "<script">, 0); if (!data->script) { g_perinterr ("Error compiling script '%.*s'\n", script, scriptlen); goto script_fail; } data->scriptobj = JS_NewScriptObject (cx, data->script); if (!data->scriptobj || !JS_AddRoot (cx, &data->scriptobj)) { goto script_fail; } }
NS_IMETHODIMP nsDOMWorkerScriptLoader::ScriptCompiler::Run() { NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); if (mRevoked) { return NS_OK; } NS_ASSERTION(!mScriptObj.ToJSObject(), "Already have a script object?!"); NS_ASSERTION(mScriptObj.IsHeld(), "Not held?!"); NS_ASSERTION(!mScriptText.IsEmpty(), "Shouldn't have empty source here!"); JSContext* cx = nsDOMThreadService::GetCurrentContext(); NS_ENSURE_STATE(cx); JSAutoRequest ar(cx); JSObject* global = JS_GetGlobalObject(cx); NS_ENSURE_STATE(global); // Because we may have nested calls to this function we don't want the // execution to automatically report errors. We let them propagate instead. uint32 oldOpts = JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_DONT_REPORT_UNCAUGHT); JSPrincipals* principal = nsDOMWorkerSecurityManager::WorkerPrincipal(); JSScript* script = JS_CompileUCScriptForPrincipals(cx, global, principal, reinterpret_cast<const jschar*> (mScriptText.BeginReading()), mScriptText.Length(), mFilename.get(), 1); JS_SetOptions(cx, oldOpts); if (!script) { return NS_ERROR_FAILURE; } mScriptObj = JS_NewScriptObject(cx, script); NS_ENSURE_STATE(mScriptObj.ToJSObject()); return NS_OK; }
/* * call-seq: * native_compile(script, filename, linenum) * * Compile +script+ with +filename+ using +linenum+ */ static VALUE native_compile(VALUE self, VALUE script, VALUE filename, VALUE linenum) { JohnsonRuntime* runtime; Data_Get_Struct(self, JohnsonRuntime, runtime); JSContext * context = johnson_get_current_context(runtime); JohnsonContext * johnson_context = OUR_CONTEXT(context); JSScript * compiled_js = JS_CompileScript( context, runtime->global, StringValuePtr(script), (size_t)StringValueLen(script), StringValueCStr(filename), (unsigned)NUM2INT(linenum) ); if(compiled_js == NULL) { if (JS_IsExceptionPending(context)) { // If there's an exception pending here, it's a syntax error. JS_GetPendingException(context, &johnson_context->ex); JS_ClearPendingException(context); } if (johnson_context->ex) { RAISE_JS_ERROR(self, johnson_context->ex); return Qnil; } } JSObject * script_object = JS_NewScriptObject(context, compiled_js); PREPARE_RUBY_JROOTS(context, 1); JROOT(script_object); JRETURN_RUBY(make_ruby_land_proxy(runtime, OBJECT_TO_JSVAL(script_object), "JSScriptProxy")); }
int js_plugin_load(const char *id, const char *url, char *errbuf, size_t errlen) { char *sbuf; struct fa_stat fs; JSContext *cx; js_plugin_t *jsp; JSObject *pobj, *gobj, *confobj; JSScript *s; char path[PATH_MAX]; jsval val; if((sbuf = fa_quickload(url, &fs, NULL, errbuf, errlen)) == NULL) return -1; cx = js_newctx(err_reporter); JS_BeginRequest(cx); /* Remove any plugin with same URL */ LIST_FOREACH(jsp, &js_plugins, jsp_link) if(!strcmp(jsp->jsp_id, id)) break; if(jsp != NULL) js_plugin_unload(cx, jsp); jsp = calloc(1, sizeof(js_plugin_t)); jsp->jsp_url = strdup(url); jsp->jsp_id = strdup(id); LIST_INSERT_HEAD(&js_plugins, jsp, jsp_link); gobj = JS_NewObject(cx, &global_class, NULL, NULL); JS_InitStandardClasses(cx, gobj); JS_DefineProperty(cx, gobj, "showtime", OBJECT_TO_JSVAL(showtimeobj), NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT); /* Plugin object */ pobj = JS_NewObject(cx, &plugin_class, NULL, gobj); JS_AddNamedRoot(cx, &pobj, "plugin"); JS_SetPrivate(cx, pobj, jsp); JS_DefineFunctions(cx, pobj, plugin_functions); /* Plugin config object */ confobj = JS_DefineObject(cx, pobj, "config", &plugin_conf_class, NULL, 0); JS_SetPrivate(cx, confobj, jsp); val = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, url)); JS_SetProperty(cx, confobj, "url", &val); if(!fa_parent(path, sizeof(path), url)) { val = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, path)); JS_SetProperty(cx, confobj, "path", &val); } JS_DefineProperty(cx, confobj, "URIRouting", BOOLEAN_TO_JSVAL(1), NULL, jsp_setEnableURIRoute, JSPROP_PERMANENT); JS_DefineProperty(cx, confobj, "search", BOOLEAN_TO_JSVAL(1), NULL, jsp_setEnableSearch, JSPROP_PERMANENT); s = JS_CompileScript(cx, pobj, sbuf, fs.fs_size, url, 1); free(sbuf); if(s != NULL) { JSObject *sobj = JS_NewScriptObject(cx, s); jsval result; JS_AddNamedRoot(cx, &sobj, "script"); JS_ExecuteScript(cx, pobj, s, &result); JS_RemoveRoot(cx, &sobj); } JS_RemoveRoot(cx, &pobj); JS_EndRequest(cx); JS_GC(cx); JS_DestroyContext(cx); return 0; }