// // InstallTriggerGlobal class initialization // nsresult NS_InitInstallTriggerGlobalClass(nsIScriptContext *aContext, void **aPrototype) { JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); JSObject *proto = nsnull; JSObject *constructor = nsnull; JSObject *global = JS_GetGlobalObject(jscontext); jsval vp; if ((PR_TRUE != JS_LookupProperty(jscontext, global, "InstallTriggerGlobal", &vp)) || !JSVAL_IS_OBJECT(vp) || ((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) || (PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), "prototype", &vp)) || !JSVAL_IS_OBJECT(vp)) { nsresult rv = InitInstallTriggerGlobalClass(jscontext, global, (void**)&proto); if (NS_FAILED(rv)) return rv; } else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp)) { proto = JSVAL_TO_OBJECT(vp); } else { return NS_ERROR_FAILURE; } if (aPrototype) *aPrototype = proto; return NS_OK; }
GF_EXPORT GF_Err gf_webvtt_js_removeCues(GF_Node *node) { GF_Err e; JSBool found; JSContext *c = node->sgprivate->scenegraph->svg_js->js_ctx; JSObject *global = node->sgprivate->scenegraph->svg_js->global; jsval fun_val; gf_sg_lock_javascript(c, GF_TRUE); found = JS_LookupProperty(c, global, "removeCues", &fun_val); if (!found || JSVAL_IS_VOID(fun_val) || !JSVAL_IS_OBJECT(fun_val) ) { e = GF_BAD_PARAM; } else { JSBool ret; uintN attr; ret = JS_GetPropertyAttributes(c, global, "removeCues", &attr, &found); if (ret == JS_TRUE && found == JS_TRUE) { jsval rval; ret = JS_CallFunctionValue(c, global, fun_val, 0, NULL, &rval); if (ret == JS_TRUE) { e = GF_OK; } else { e = GF_BAD_PARAM; } } else { e = GF_BAD_PARAM; } } gf_sg_lock_javascript(c, GF_FALSE); return e; }
static JSBool SMJS_FUNCTION(widget_invoke_message) { jsval oval; GF_WidgetMessage *msg = NULL; SMJS_OBJ SMJS_ARGS GF_WidgetInterfaceInstance *bifce = (GF_WidgetInterfaceInstance *)SMJS_GET_PRIVATE(c, obj); if (!bifce) return JS_FALSE; SMJS_SET_RVAL( JSVAL_NULL ); if (!JSVAL_IS_OBJECT(argv[0])) return JS_FALSE; if (JSVAL_IS_NULL(argv[0])) return JS_FALSE; msg = (GF_WidgetMessage *)SMJS_GET_PRIVATE(c, JSVAL_TO_OBJECT(argv[0]) ); if (!msg) return JS_FALSE; /*look for JS Callback "invoke" in the widget manager script*/ if (JS_LookupProperty(c, bifce->ifce->obj, "invoke", &oval)==JS_TRUE) { if (JSVAL_IS_OBJECT(oval)) { JS_CallFunctionValue(bifce->wid->widget->wm->ctx, bifce->ifce->obj, oval, argc, argv, SMJS_GET_RVAL ); } } return JS_TRUE; }
static JSBool DriverManager_getDriver(JSContext *cx, unsigned argc, jsval *vp) { jsval drivers; JS_LookupProperty(cx, JS_THIS_OBJECT(cx, vp), "drivers", &drivers); // FIXME check return code; check that drivers is an array JSObject *obj = JSVAL_TO_OBJECT(drivers); uint32_t len, i; JS_GetArrayLength(cx, obj, &len); for (i = 0; i < len; i++) { jsval driver, rval; jsval acceptsURL_argv[] = {JS_ARGV(cx, vp)[0]}; JS_GetElement(cx, obj, i, &driver); JS_CallFunctionName(cx, JSVAL_TO_OBJECT(driver), "acceptsURL", 1, &acceptsURL_argv[0], &rval); // FIXME check return value JSBool acceptsURL; JS_ValueToBoolean(cx, rval, &acceptsURL); // FIXME check ret val if (acceptsURL) { JS_SET_RVAL(cx, vp, driver); return JS_TRUE; } } JS_ReportError(cx, "No suitable driver"); JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_FALSE; }
/* |callee| requires a usage string provided by JS_DefineFunctionsWithHelp. */ void ReportUsageError(JSContext *cx, JSObject *callee, const char *msg) { const char *usageStr = "usage"; PropertyName *usageAtom = js_Atomize(cx, usageStr, strlen(usageStr))->asPropertyName(); DebugOnly<Shape *> shape = callee->nativeLookup(cx, NameToId(usageAtom)); JS_ASSERT(!shape->configurable()); JS_ASSERT(!shape->writable()); JS_ASSERT(shape->hasDefaultGetter()); jsval usage; if (!JS_LookupProperty(cx, callee, "usage", &usage)) return; if (JSVAL_IS_VOID(usage)) { JS_ReportError(cx, "%s", msg); } else { JSString *str = JSVAL_TO_STRING(usage); JS::Anchor<JSString *> a_str(str); const jschar *chars = JS_GetStringCharsZ(cx, str); if (!chars) return; JS_ReportError(cx, "%s. Usage: %hs", msg, chars); } }
static JSObject * define_JavaPackage(JSContext *cx, JSObject *parent_obj, const char *obj_name, const char *path, int flags, int access) { JSObject *package_obj, *obj; JavaPackage_Private *package; jsval v; /* * Expose the same JSObject for Packages.java and java. * "java" will be defined during the java package initialization stage. * "Packages.java" will be lazily resolved by JavaPackage_resolve. * Ditto for sun and netscape. * See bugzilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=248409. */ if (!strcmp(obj_name, path) && (obj = JS_GetParent(cx, parent_obj)) && JS_LookupProperty(cx, obj, obj_name, &v) && !JSVAL_IS_PRIMITIVE(v)) { if (!JS_DefineProperty(cx, parent_obj, obj_name, v, NULL, NULL, JSPROP_PERMANENT | access)) { return NULL; } package_obj = JSVAL_TO_OBJECT(v); return package_obj; } package_obj = JS_DefineObject(cx, parent_obj, obj_name, &JavaPackage_class, 0, JSPROP_PERMANENT | access); if (!package_obj) return NULL; /* Attach private, native data to the JS object */ package = (JavaPackage_Private *)JS_malloc(cx, sizeof(JavaPackage_Private)); if (!package) { JS_DeleteProperty(cx, parent_obj, obj_name); return NULL; } JS_SetPrivate(cx, package_obj, (void *)package); if (path) package->path = JS_strdup(cx, path); else package->path = ""; package->flags = flags; /* Check for OOM */ if (!package->path) { JS_DeleteProperty(cx, parent_obj, obj_name); JS_free(cx, package); return NULL; } return package_obj; }
///// JavaScript Initialization Method JSObject *ofxJSTrueTypeFont::JSInit(JSContext *cx, JSObject *obj) { if (obj==NULL) obj = JS_GetGlobalObject(cx); jsval oldobj; if (JS_TRUE == JS_LookupProperty(cx, obj, ofxJSTrueTypeFont::_jsClass.name, &oldobj) && JSVAL_IS_OBJECT(oldobj)) return JSVAL_TO_OBJECT(oldobj); return JS_InitClass(cx, obj, NULL, &ofxJSTrueTypeFont::_jsClass, ofxJSTrueTypeFont::JSConstructor, 0, NULL, ofxJSTrueTypeFont::_JSFunctionSpec, NULL, NULL); }
static JSBool DriverManager_getConnection(JSContext *cx, unsigned argc, jsval *vp) { jsval drivers; JS_LookupProperty(cx, JS_THIS_OBJECT(cx, vp), "drivers", &drivers); // FIXME check return code; check that drivers is an array JSObject *obj = JSVAL_TO_OBJECT(drivers); uint32_t len, i; JS_GetArrayLength(cx, obj, &len); jsval connect_argv[2] = {JS_ARGV(cx, vp)[0]}; if (argc == 2) { /* Caller passed "info" object, so we forward it as-is */ connect_argv[1] = JS_ARGV(cx, vp)[1]; } else { JSObject *info = JS_NewObject(cx, NULL, NULL, NULL); // FIXME root it to avoid GC if (argc > 1) JS_DefineProperty(cx, info, "user", JS_ARGV(cx, vp)[1], NULL, NULL, JSPROP_ENUMERATE); if (argc > 2) JS_DefineProperty(cx, info, "password", JS_ARGV(cx, vp)[2], NULL, NULL, JSPROP_ENUMERATE); connect_argv[1] = OBJECT_TO_JSVAL(info); }; jsval reason = JSVAL_NULL; for (i = 0; i < len; i++) { jsval driver, rval; JS_GetElement(cx, obj, i, &driver); if (!JS_CallFunctionName(cx, JSVAL_TO_OBJECT(driver), "connect", 2, &connect_argv[0], &rval)) { if (JSVAL_IS_NULL(reason)) JS_GetPendingException(cx, &reason); continue; } if (JSVAL_IS_NULL(rval)) continue; JS_SET_RVAL(cx, vp, rval); return JS_TRUE; } if (JSVAL_IS_NULL(reason)) { JSString *url_str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]); // FIXME check return value // FIXME root url_str (protect from GC) -> https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_Reference/JS_ValueToString char *url = JS_EncodeString(cx, url_str); JS_ReportError(cx, "No suitable driver found for %s", url); JS_free(cx, url); } else JS_SetPendingException(cx, reason); JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_FALSE; }
static JSBool DriverManager_registerDriver(JSContext *cx, unsigned argc, jsval *vp) { jsval drivers; JS_LookupProperty(cx, JS_THIS_OBJECT(cx, vp), "drivers", &drivers); // FIXME check return code; check that drivers is an array JSObject *obj = JSVAL_TO_OBJECT(drivers); uint32_t len; JS_GetArrayLength(cx, obj, &len); JS_SetElement(cx, obj, len, JS_ARGV(cx, vp)); JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_TRUE; }
JSBool SMJS_FUNCTION(widget_get_attention) { jsval fval; SMJS_OBJ GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid) return JS_TRUE; if ((JS_LookupProperty(c, wid->widget->wm->obj, "getAttention", &fval)==JS_TRUE) && JSVAL_IS_OBJECT(fval)) { jsval args[1]; args[0] = OBJECT_TO_JSVAL(wid->obj); JS_CallFunctionValue(c, wid->widget->wm->obj, fval, 1, args, SMJS_GET_RVAL); } return JS_TRUE; }
static JSBool perlsub_construct( JSContext *cx, DEFJSFSARGS_ ) { dTHX; DECJSFSARGS; JSObject *func = JSVAL_TO_OBJECT(JS_ARGV_CALLEE(argv)); SV *callee = (SV *)JS_GetPrivate(cx, func); SV *caller = NULL; #if JS_VERSION < 185 JSObject *This = JSVAL_TO_OBJECT(argv[-1]); #else JSObject *This = JS_NewObjectForConstructor(cx, vp); #endif JSObject *proto = JS_GetPrototype(cx, This); PJS_DEBUG1("Want construct, This is a %s", PJS_GET_CLASS(cx, This)->name); if(PJS_GET_CLASS(cx, proto) == &perlpackage_class || ( JS_LookupProperty(cx, func, "prototype", &argv[-1]) && JSVAL_IS_OBJECT(argv[-1]) && !JSVAL_IS_NULL(argv[-1]) && (proto = JS_GetPrototype(cx, JSVAL_TO_OBJECT(argv[-1]))) && strEQ(PJS_GET_CLASS(cx, proto)->name, PJS_PACKAGE_CLASS_NAME)) ) { SV *rsv = NULL; char *pkgname = PJS_GetPackageName(aTHX_ cx, proto); #if JS_VERSION >= 185 JSAutoByteString bytes; bytes.initBytes(pkgname); #endif caller = newSVpv(pkgname, 0); argv[-1] = OBJECT_TO_JSVAL(This); if(!PJS_Call_sv_with_jsvals_rsv(aTHX_ cx, obj, callee, caller, argc, argv, &rsv, G_SCALAR)) return JS_FALSE; if(SvROK(rsv) && sv_derived_from(rsv, pkgname)) { JSObject *newobj = PJS_NewPerlObject(aTHX_ cx, JS_GetParent(cx, func), rsv); *rval = OBJECT_TO_JSVAL(newobj); return JS_TRUE; } JS_ReportError(cx, "%s's constructor don't return an object", SvPV_nolen(caller)); } else JS_ReportError(cx, "Can't use as a constructor"); // Yet! ;-) return JS_FALSE; }
void widget_on_interface_bind(GF_WidgetInterfaceInstance *ifce, Bool unbind) { jsval funval, rval, argv[1]; const char *fun_name = unbind ? "onInterfaceUnbind" : "onInterfaceBind"; if (!ifce || !ifce->wid || !ifce->wid->scene_context) return; /*look for JS Callback "invoke" in the widget manager script*/ if (JS_LookupProperty(ifce->wid->scene_context, ifce->wid->scene_obj, fun_name, &funval)!=JS_TRUE) return; if (!JSVAL_IS_OBJECT(funval)) return; widget_interface_js_bind(ifce->wid->widget->wm->ctx, ifce); argv[0] = OBJECT_TO_JSVAL(ifce->obj); JS_CallFunctionValue(ifce->wid->widget->wm->ctx, ifce->ifce->obj, funval, 1, argv, &rval); }
// JavaScript Initialization Method JSObject *JSDOMProcessingInstruction::JSInit(JSContext *cx, JSObject *obj) { if (obj==NULL) obj = JS_GetGlobalObject(cx); jsval oldobj; if (JS_TRUE == JS_LookupProperty(cx, obj, JSDOMProcessingInstruction::_jsClass.name, &oldobj) && JSVAL_IS_OBJECT(oldobj)) return JSVAL_TO_OBJECT(oldobj); JSObject *newobj = JS_InitClass(cx, obj, NULL, &JSDOMProcessingInstruction::_jsClass, NULL, 0, JSDOMProcessingInstruction::_JSPropertySpec, NULL, NULL, NULL); if (newobj) { InitNodeObject(cx, newobj); } return newobj; }
JSBool perlsub_as_constructor( JSContext *cx, JSObject *obj, pjsid id, DEFSTRICT_ jsval *vp ) { // dTHX; const char *key; if(!PJSID_IS(STRING, id)) { return JS_TRUE; } #if JS_VERSION < 185 key = JS_GetStringBytes(PJSID_TO(STRING, id)); #else JSAutoByteString bytes(cx, PJSID_TO(STRING, id)); key = bytes.ptr(); #endif if(strEQ(key, "constructor")) { JSObject *constructor; if(JSVAL_IS_OBJECT(*vp) && (constructor = JSVAL_TO_OBJECT(*vp)) && PJS_GET_CLASS(cx, constructor) == &perlsub_class) { /* TODO: Change the constructor 'name' */ jsval temp; JSObject *stash = JS_GetPrototype(cx, obj); JS_SetPrototype(cx, stash, JS_GetPrototype(cx, constructor)); JS_SetPrototype(cx, constructor, stash); JS_DefineProperty(cx, constructor, "prototype", OBJECT_TO_JSVAL(obj), NULL, NULL, 0); JS_LookupProperty(cx, obj, "__PACKAGE__", &temp); // warn("Constructor set for %s\n", JS_GetStringBytes(JSVAL_TO_STRING(temp))); return JS_TRUE; } else { JS_ReportError(cx, "Invalid constructor type"); return JS_FALSE; } } else warn ("Opps: setting %s?\n", key); return JS_TRUE; }
// JavaScript Initialization Method JSObject *JSDOMText::JSInit(JSContext *cx, JSObject *obj) { if (obj==NULL) obj = JS_GetGlobalObject(cx); jsval oldobj; if (JS_TRUE == JS_LookupProperty(cx, obj, JSDOMText::_jsClass.name, &oldobj) && JSVAL_IS_OBJECT(oldobj)) return JSVAL_TO_OBJECT(oldobj); JSObject *newobj = JS_InitClass(cx, obj, NULL, &JSDOMText::_jsClass, NULL, 0, NULL, NULL, NULL, NULL); if (newobj) { InitCharacterDataObject(cx, newobj); } return newobj; }
GF_EXPORT GF_Err gf_webvtt_js_addCue(GF_Node *node, const char *id, const char *start, const char *end, const char *settings, const char *payload) { GF_Err e; JSBool found; JSContext *c = node->sgprivate->scenegraph->svg_js->js_ctx; JSObject *global = node->sgprivate->scenegraph->svg_js->global; jsval fun_val; gf_sg_lock_javascript(c, GF_TRUE); found = JS_LookupProperty(c, global, "addCue", &fun_val); if (!found || JSVAL_IS_VOID(fun_val) || !JSVAL_IS_OBJECT(fun_val) ) { e = GF_BAD_PARAM; } else { JSBool ret; uintN attr; ret = JS_GetPropertyAttributes(c, global, "addCue", &attr, &found); if (ret == JS_TRUE && found == JS_TRUE) { jsval rval; jsval argv[5]; argv[0] = STRING_TO_JSVAL( JS_NewStringCopyZ(c, (id ? id : "")) ); argv[1] = STRING_TO_JSVAL( JS_NewStringCopyZ(c, (start ? start : "")) ); argv[2] = STRING_TO_JSVAL( JS_NewStringCopyZ(c, (end ? end : "")) ); argv[3] = STRING_TO_JSVAL( JS_NewStringCopyZ(c, (settings ? settings : "")) ); argv[4] = STRING_TO_JSVAL( JS_NewStringCopyZ(c, (payload ? payload : "")) ); ret = JS_CallFunctionValue(c, global, fun_val, 5, argv, &rval); //ret = JS_CallFunctionName(c, global, "addCue", 5, argv, &rval); if (ret == JS_TRUE) { e = GF_OK; } else { e = GF_BAD_PARAM; } } else { e = GF_BAD_PARAM; } } gf_sg_lock_javascript(c, GF_FALSE); return e; }
// JavaScript Initialization Method JSObject *JSDOMDocument::JSInit(JSContext *cx, JSObject *obj) { if (voiceglue_loglevel() >= LOG_DEBUG) voiceglue_log ((char) LOG_DEBUG, "JSDOMDocument::JSInit() started"); if (obj==NULL) obj = JS_GetGlobalObject(cx); jsval oldobj; if (JS_TRUE == JS_LookupProperty (cx, obj, JSDOMDocument::_jsClass.name, &oldobj) && JSVAL_IS_OBJECT(oldobj)) { if (voiceglue_loglevel() >= LOG_DEBUG) voiceglue_log ((char) LOG_DEBUG, "Returning previous JSDOMDocument object"); return JSVAL_TO_OBJECT(oldobj); }; JSObject *newobj = JS_InitClass (cx, obj, NULL, &JSDOMDocument::_jsClass, NULL, 0, JSDOMDocument::_JSPropertySpec, JSDOMDocument::_JSFunctionSpec, NULL, NULL); if (newobj) { if (voiceglue_loglevel() >= LOG_DEBUG) voiceglue_log ((char) LOG_DEBUG, "JS_InitClass(JSDOMDocument) OK, do: InitNodeObject()"); InitNodeObject(cx, newobj); } else { if (voiceglue_loglevel() >= LOG_ERR) voiceglue_log ((char) LOG_ERR, "JS_InitClass(JSDOMDocument) failed"); }; return newobj; }
JSBool SMJS_FUNCTION(widget_show_notification) { jsval fval; SMJS_OBJ SMJS_ARGS GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid) return JS_TRUE; if ((JS_LookupProperty(c, wid->widget->wm->obj, "showNotification", &fval)==JS_TRUE) && JSVAL_IS_OBJECT(fval)) { jsval *vars; u32 i; vars = gf_malloc(sizeof(jsval)*(argc+1)); vars[0] = OBJECT_TO_JSVAL(wid->obj); for (i=0; i<argc; i++) vars[i+1] = argv[i]; JS_CallFunctionValue(c, wid->widget->wm->obj, fval, argc+1, vars, SMJS_GET_RVAL); } return JS_TRUE; }
void DEBUG_CheckForComponentsInScope(JSContext* cx, JSObject* obj, JSObject* startingObj, JSBool OKIfNotInitialized, XPCJSRuntime* runtime) { if(OKIfNotInitialized) return; if(!(JS_GetOptions(cx) & JSOPTION_PRIVATE_IS_NSISUPPORTS)) return; const char* name = runtime->GetStringName(XPCJSRuntime::IDX_COMPONENTS); jsval prop; if(JS_LookupProperty(cx, obj, name, &prop) && !JSVAL_IS_PRIMITIVE(prop)) return; // This is pretty much always bad. It usually means that native code is // making a callback to an interface implemented in JavaScript, but the // document where the JS object was created has already been cleared and the // global properties of that document's window are *gone*. Generally this // indicates a problem that should be addressed in the design and use of the // callback code. NS_ERROR("XPConnect is being called on a scope without a 'Components' property! (stack and details follow)"); printf("The current JS stack is:\n"); xpc_DumpJSStack(cx, JS_TRUE, JS_TRUE, JS_TRUE); printf("And the object whose scope lacks a 'Components' property is:\n"); js_DumpObject(startingObj); JSObject *p = startingObj; while(p->isWrapper()) { p = p->getProxyPrivate().toObjectOrNull(); if(!p) break; printf("which is a wrapper for:\n"); js_DumpObject(p); } }
void SG_jscore__check_module_dags(SG_context *pCtx, JSContext *cx, JSObject *glob, const char *reponame) { jsval args[2]; JSBool js_ok; jsval rval; JSString *pjs; jsval fo = JSVAL_VOID; if (gpJSCoreGlobalState->bSkipModules || (! gpJSCoreGlobalState->pPathToModules)) return; SG_ERR_CHECK( _sg_jscore__install_modules(pCtx, cx, glob, NULL) ); SG_JS_NULL_CHECK( (pjs = JS_NewStringCopyZ(cx, reponame)) ); args[0] = STRING_TO_JSVAL(pjs); SG_JS_NULL_CHECK( (pjs = JS_NewStringCopyZ(cx, SG_pathname__sz(gpJSCoreGlobalState->pPathToModules))) ); args[1] = STRING_TO_JSVAL(pjs); if (! JS_LookupProperty(cx, glob, "checkModuleDags", &fo)) { SG_ERR_CHECK_CURRENT; SG_ERR_THROW2(SG_ERR_JS, (pCtx, "lookup of checkModuleDags failed")); } if (!JSVAL_IS_VOID(fo)) { js_ok = JS_CallFunctionName(cx, glob, "checkModuleDags", SG_NrElements(args), args, &rval); SG_ERR_CHECK_CURRENT; if(!js_ok) SG_ERR_THROW2(SG_ERR_JS, (pCtx, "An error occurred initializing modules: call to JavaScript checkModuleDags() failed")); } fail: ; }
static JSBool SMJS_FUNCTION(widget_call_message_reply_callback) { JSObject *list; jsval *vals, fval; u32 i, count; SMJS_OBJ SMJS_ARGS GF_WidgetMessage *msg = SMJS_GET_PRIVATE(c, obj); if (!msg || !argc || !JSVAL_IS_OBJECT(argv[0]) ) return JS_FALSE; if ((JS_LookupProperty(c, obj, "replyCallback", &fval)==JS_TRUE) && JSVAL_IS_OBJECT(fval)) { list = JSVAL_TO_OBJECT(argv[0]); JS_GetArrayLength(c, list, (jsuint*) &count); vals = gf_malloc(sizeof(jsval)*(count+1)); vals[0] = OBJECT_TO_JSVAL(obj); for (i=0; i<count; i++) { JS_GetElement(c, list, (jsint) i, &vals[i+1]); } JS_CallFunctionValue(c, obj, fval, count, vals, SMJS_GET_RVAL); gf_free(vals); } return JS_TRUE; }
void DEBUG_CheckForComponentsInScope(JSContext* cx, JSObject* obj, JSBool OKIfNotInitialized, XPCJSRuntime* runtime) { if(OKIfNotInitialized) return; const char* name = runtime->GetStringName(XPCJSRuntime::IDX_COMPONENTS); jsval prop; if(JS_LookupProperty(cx, obj, name, &prop) && !JSVAL_IS_PRIMITIVE(prop)) return; // This is pretty much always bad. It usually means that native code is // making a callback to an interface implemented in JavaScript, but the // document where the JS object was created has already been cleared and the // global properties of that document's window are *gone*. Generally this // indicates a problem that should be addressed in the design and use of the // callback code. #ifdef I_FOOLISHLY_WANT_TO_IGNORE_THIS_LIKE_THE_OTHER_CRAP_WE_PRINTF NS_WARNING("XPConnect is being called on a scope without a 'Components' property!"); #else NS_ERROR("XPConnect is being called on a scope without a 'Components' property!"); #endif }
/* Wrap a JS value to export into perl * Returns a new SV, REFCNT_dec is caller's responsability */ JSBool PJS_ReflectJS2Perl( pTHX_ JSContext *cx, jsval value, SV** sv, int full ) { if(JSVAL_IS_PRIMITIVE(value)) { *sv = PrimJSVALToSV(aTHX_ cx, value); if(*sv) return JS_TRUE; } else if(JSVAL_IS_OBJECT(value)) { PJS_Context *pcx = PJS_GET_CONTEXT(cx); JSObject *object = JSVAL_TO_OBJECT(value); JSClass *clasp = PJS_GET_CLASS(cx, object); const char *classname = clasp->name; JSObject *passport; SV *wrapper; SV *box; char hkey[32]; jsval temp = JSVAL_VOID; snprintf(hkey, 32, "%p", (void *)object); PJS_DEBUG2("Wrapping a %s(%s)\n", classname, hkey); if(PJS_getFlag(pcx, "ConvertRegExp") && strEQ(classname, "RegExp")) { jsval src; char *str; if(JS_CallFunctionName(cx, object, "toSource", 0, NULL, &src) && (str = JS_GetStringBytes(JS_ValueToString(cx, src))) ) { dSP; SV *tmp = newSVpvf("qr%s", str); eval_sv(tmp, G_SCALAR); sv_free(tmp); // Don't leak SPAGAIN; tmp = POPs; PUTBACK; if(!SvTRUE(ERRSV)) { *sv = SvREFCNT_inc_simple_NN(tmp); return JS_TRUE; } } return JS_FALSE; } if(IS_PERL_CLASS(clasp)) { /* IS_PERL_CLASS means actual perl object is there */ SV *priv = (SV *)JS_GetPrivate(cx, object); if(priv && SvOK(priv) && SvROK(priv)) { *sv = SvREFCNT_inc_simple_NN(priv); return JS_TRUE; } croak("A private %s?!\n", classname); return JS_FALSE; } /* Common JSObject case */ /* Check registered perl visitors */ JS_LookupProperty(cx, pcx->pvisitors, hkey, &temp); if(temp != JSVAL_VOID) { /* Already registered, so exits a reference in perl space * _must_ hold a PASSPORT */ assert(JSVAL_TO_OBJECT(temp) == object); box = PJS_GetPassport(aTHX_ cx, object); SvREFCNT_inc_void_NN(box); /* In perl should be one more */ PJS_DEBUG1("Cached!: %s\n", hkey); } else { /* Check if with a PASSPORT */ JS_LookupPropertyWithFlags(cx, object, PJS_PASSPORT_PROP, 0, &temp); if(JSVAL_IS_OBJECT(temp) && (passport = JSVAL_TO_OBJECT(temp)) && PJS_GET_CLASS(cx, passport) == &passport_class && JS_GetReservedSlot(cx, passport, 0, &temp) && object == (JSObject *)JSVAL_TO_PRIVATE(temp) ) { /* Yes, reentering perl */ box = (SV *)JS_GetPrivate(cx, passport); /* Here we don't increment refcount, the ownership in passport is * transferred to perl land. */ PJS_DEBUG1("Reenter: %s\n", hkey); } else { /* No, first time, must wrap the object */ SV *boxref; const char *package; SV *robj = newSV(0); SV *rjsv = newSV(0); if (JS_ObjectIsFunction(cx, object)) package = PJS_FUNCTION_PACKAGE; else if(JS_IsArrayObject(cx, object)) package = PJS_ARRAY_PACKAGE; else if(strEQ(classname, PJS_PACKAGE_CLASS_NAME)) package = PJS_STASH_PACKAGE; #if JS_HAS_XML_SUPPORT else if(strEQ(classname, "XML")) package = PJS_XMLOBJ_PACKAGE; #endif else if(strEQ(classname, "Error")) package = PJS_ERROR_PACKAGE; else { SV **sv = hv_fetch(get_hv(NAMESPACE"ClassMap", 1), classname, strlen(classname), 0); if(sv) package = SvPV_nolen(*sv); else package = PJS_OBJECT_PACKAGE; } sv_setref_pv(robj, PJS_RAW_OBJECT, (void*)object); sv_setref_iv(rjsv, PJS_RAW_JSVAL, (IV)value); boxref = PJS_CallPerlMethod(aTHX_ cx, "__new", sv_2mortal(newSVpv(package, 0)), // package sv_2mortal(robj), // content sv_2mortal(rjsv), // jsval NULL ); if(!boxref) return JS_FALSE; if(!SvOK(boxref) || !sv_derived_from(boxref, PJS_BOXED_PACKAGE)) croak("PJS_Assert: Contructor must return a "NAMESPACE"Boxed"); /* Create a new PASSPORT */ passport = JS_NewObject(cx, &passport_class, NULL, object); if(!passport || !JS_DefineProperty(cx, object, PJS_PASSPORT_PROP, OBJECT_TO_JSVAL(passport), NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT)) return JS_FALSE; box = SvRV(boxref); /* boxref is mortal, so we need to increment its rc, at end of * scope, PASSPORT owns created box */ JS_SetPrivate(cx, passport, (void *)SvREFCNT_inc_simple_NN(box)); JS_SetReservedSlot(cx, passport, 0, PRIVATE_TO_JSVAL(object)); PJS_DEBUG2("New boxed: %s brc: %d\n", hkey, SvREFCNT(box)); } /* Root object adding it to pvisitors list, will be unrooted by * jsc_free_root at Boxed DESTROY time */ JS_DefineProperty(cx, pcx->pvisitors, hkey, value, NULL, NULL, 0); } /* Here the RC of box in PASSPORT reflects wrapper's ownership */ if(full && PJS_getFlag(pcx, "AutoTie") && (strEQ(classname, "Object") || strEQ(classname, "Array")) ) { /* Return tied */ AV *avbox = (AV *)SvRV(box); SV **last; SV *tied; SV *tier; if(strEQ(classname, "Array")) { last = av_fetch(avbox, 6, 1); if(last && SvOK(*last) && SvROK(*last)) { // Cached *sv = newSVsv(*last); sv_free(box); /* Hard copy 'sv' owns the reference */ return JS_TRUE; } tied = (SV *)newAV(); } else { // Object last = av_fetch(avbox, 5, 1); if(last && SvOK(*last) && SvROK(*last)) { // Cached *sv = newSVsv(*last); sv_free(box); /* Hard copy 'sv' owns the reference */ return JS_TRUE; } tied = (SV *)newHV(); } /* hv_magic below own a reference to box, we use an explicit path, * to make clear that to perl land only one reference is given */ tier = newRV_inc(box); hv_magic((HV *)tied, (GV *)tier, PERL_MAGIC_tied); sv_free(tier); wrapper = newRV_noinc(tied); /* Don't leak the hidden tied variable */ /* Save in cache a weaken copy, the cache itself dosn't hold a reference */ sv_setsv(*last, wrapper); sv_rvweaken(*last); PJS_DEBUG1("Return tied for %s\n", SvPV_nolen(tier)); } else { wrapper = newRV_noinc(box); /* Transfer ownership to wrapper */ #if PERL_VERSION < 9 sv_bless(wrapper, SvSTASH(box)); #endif } *sv = wrapper; return JS_TRUE; } return JS_FALSE; }
nsresult WebCL_variantToImageFormat (JSContext *cx, nsIVariant* aVariant, cl_image_format& aResultOut) { D_METHOD_START; NS_ENSURE_ARG_POINTER (aVariant); nsresult rv; PRUint16 variantType = 0; rv = aVariant->GetDataType (&variantType); if ( !(variantType == nsIDataType::VTYPE_INTERFACE || variantType == nsIDataType::VTYPE_INTERFACE_IS)) { return NS_ERROR_INVALID_ARG; } if (!cx) { nsCOMPtr<nsIThreadJSContextStack> stack = do_GetService ("@mozilla.org/js/xpc/ContextStack;1", &rv); NS_ENSURE_SUCCESS (rv, rv); cx = stack->GetSafeJSContext (); NS_ENSURE_TRUE (cx, NS_ERROR_FAILURE); } nsCOMPtr<nsIXPConnect> xpc = do_GetService (nsIXPConnect::GetCID (), &rv); NS_ENSURE_SUCCESS (rv, rv); js::Value jsVal; rv = xpc->VariantToJS(cx, JS_GetGlobalObject(cx), aVariant, &jsVal); NS_ENSURE_SUCCESS (rv, rv); NS_ENSURE_TRUE (jsVal.isObject (), NS_ERROR_INVALID_ARG); JSObject* jsObj = jsVal.toObjectOrNull ();; if (jsObj && js::IsObjectProxy (jsObj)) { jsObj = js::UnwrapObject (jsObj); } if (!jsObj) { return NS_ERROR_INVALID_ARG; } js::Value propChannelOrder; if (!JS_LookupProperty (cx, jsObj, "channelOrder", &propChannelOrder)) { if (!JS_LookupProperty (cx, jsObj, "image_channel_order", &propChannelOrder)) { D_LOG (LOG_LEVEL_ERROR, "Failed to read channel order property."); return NS_ERROR_INVALID_ARG; } } js::Value propChannelDataType; if (!JS_LookupProperty (cx, jsObj, "channelDataType", &propChannelDataType)) { if (!JS_LookupProperty (cx, jsObj, "image_channel_data_type", &propChannelDataType)) { D_LOG (LOG_LEVEL_ERROR, "Failed to read channel data type property."); return NS_ERROR_INVALID_ARG; } } aResultOut.image_channel_order = propChannelOrder.toInt32 (); aResultOut.image_channel_data_type = propChannelDataType.toInt32 (); return NS_OK; }
/* * Pre-define a hierarchy of JavaPackage objects. * Pre-defining a Java package at initialization time is not necessary, but * it will make package lookup faster and, more importantly, will avoid * unnecessary network accesses if classes are being loaded over the network. */ static JSBool pre_define_java_packages(JSContext *cx, JSObject *global_obj, JavaPackageDef *predefined_packages) { JSBool package_exists; JSObject *parent_obj; JavaPackageDef *package_def; char *simple_name, *cp, *package_name, *path; int flags; if (!predefined_packages) return JS_TRUE; /* Iterate over all pre-defined Java packages */ for (package_def = predefined_packages; package_def->name; package_def++) { #if HAVE_STRTOK_R char *nextstr; #endif package_name = path = NULL; parent_obj = global_obj; package_name = strdup(package_def->name); if (!package_name) goto out_of_memory; /* Walk the chain of JavaPackage objects to get to the parent of the rightmost sub-package in the fully-qualified package name. */ for (simple_name = STRTOK_1ST(package_name, ".", nextstr); simple_name /*1*/; simple_name = STRTOK_OTHER(".", nextstr)) { jsval v; if (!simple_name) { JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, JSJMSG_DOUBLE_SHIPPING, package_name); goto error; } /* Check to see if the sub-package already exists */ quiet_resolve_failure = JS_TRUE; package_exists = JS_LookupProperty(cx, parent_obj, simple_name, &v) && JSVAL_IS_OBJECT(v); quiet_resolve_failure = JS_FALSE; if (package_exists) { parent_obj = JSVAL_TO_OBJECT(v); continue; } /* New package objects should only be created at the terminal sub-package in a fully-qualified package-name */ if (STRTOK_OTHER(".", nextstr)) { JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, JSJMSG_BAD_PACKAGE_PREDEF, package_def->name); goto error; } if (package_def->path) { path = strdup(package_def->path); if (!path) goto out_of_memory; } else { /* * The default path is specified, so create it from the * fully-qualified package name. */ path = strdup(package_def->name); if (!path) goto out_of_memory; /* Transform package name, e.g. "java.lang" ==> "java/lang" */ for (cp = path; *cp != '\0'; cp++) { if (*cp == '.') *cp = '/'; } } flags = package_def->flags; parent_obj = define_JavaPackage(cx, parent_obj, simple_name, path, flags, package_def->access); if (!parent_obj) goto error; free(path); break; } free(package_name); } return JS_TRUE; out_of_memory: JS_ReportOutOfMemory(cx); error: JS_FREE_IF(cx, package_name); JS_FREE_IF(cx, path); return JS_FALSE; }
static void _sg_jscore__install_modules(SG_context * pCtx, JSContext *cx, JSObject *glob, const SG_vhash *pServerConfig) { SG_pathname *pModuleDirPath = NULL; SG_rbtree_iterator *pModuleDir = NULL; SG_rbtree *pModules = NULL; const char *szModuleDir = NULL; SG_bool ok = SG_FALSE; SG_uint32 len_js = 0; jsval rval; char *psz_js = NULL; jsval fo = JSVAL_VOID; JSObject * jsoServerConfig = NULL; if (gpJSCoreGlobalState->bSkipModules) return; SG_ERR_CHECK( _modulesInstalled(pCtx, cx, glob, &ok) ); if (ok) return; if (! gpJSCoreGlobalState->pPathToDispatchDotJS) return; SG_ERR_CHECK( _setModulesInstalled(pCtx, cx, glob) ); SG_ERR_CHECK( sg_read_entire_file(pCtx, gpJSCoreGlobalState->pPathToDispatchDotJS, &psz_js, &len_js) ); if(!JS_EvaluateScript(cx, glob, psz_js, len_js, "dispatch.js", 1, &rval)) { SG_ERR_CHECK_CURRENT; SG_ERR_THROW2(SG_ERR_JS, (pCtx, "An error occurred evaluating dispatch.js!")); } SG_NULLFREE(pCtx, psz_js); // Call init function in dispatch.js if(pServerConfig) { jsval arg; JSBool js_ok; jsval rval2; SG_JS_NULL_CHECK( jsoServerConfig = JS_NewObject(cx, NULL, NULL, NULL) ); SG_ERR_CHECK( sg_jsglue__copy_vhash_into_jsobject(pCtx, cx, pServerConfig, jsoServerConfig) ); arg = OBJECT_TO_JSVAL(jsoServerConfig); js_ok = JS_CallFunctionName(cx, glob, "init", 1, &arg, &rval2); SG_ERR_CHECK_CURRENT; if(!js_ok) SG_ERR_THROW2(SG_ERR_JS, (pCtx, "An error occurred initializing JavaScript framework: call to JavaScript init() failed")); jsoServerConfig = NULL; } // Load core. SG_ERR_CHECK( _loadModuleDir(pCtx, gpJSCoreGlobalState->pPathToCore, "core", cx, glob) ); // Load modules. SG_ERR_CHECK( SG_dir__list(pCtx, gpJSCoreGlobalState->pPathToModules, NULL, NULL, NULL, &pModules) ); SG_ERR_CHECK( SG_rbtree__iterator__first(pCtx, &pModuleDir, pModules, &ok, &szModuleDir, NULL) ); SG_ERR_CHECK( SG_PATHNAME__ALLOC__COPY(pCtx, &pModuleDirPath, gpJSCoreGlobalState->pPathToModules) ); while(ok) { if (szModuleDir[0] != '.') { SG_fsobj_stat fss; SG_ERR_CHECK( SG_pathname__append__from_sz(pCtx, pModuleDirPath, szModuleDir) ); SG_ERR_CHECK( SG_fsobj__stat__pathname(pCtx, pModuleDirPath, &fss) ); if (fss.type & SG_FSOBJ_TYPE__DIRECTORY) // dot paths? { SG_ERR_CHECK( _loadModuleDir(pCtx, pModuleDirPath, szModuleDir, cx, glob) ); } SG_ERR_CHECK( SG_pathname__remove_last(pCtx, pModuleDirPath) ); } SG_ERR_CHECK( SG_rbtree__iterator__next(pCtx, pModuleDir, &ok, &szModuleDir, NULL) ); } if (! JS_LookupProperty(cx, glob, "initModules", &fo)) { SG_ERR_CHECK_CURRENT; SG_ERR_THROW2(SG_ERR_JS, (pCtx, "lookup of initModules() failed")); } if (!JSVAL_IS_VOID(fo)) if (! JS_CallFunctionName(cx, glob, "initModules", 0, NULL, &rval)) { SG_ERR_CHECK_CURRENT; SG_ERR_THROW2(SG_ERR_JS, (pCtx, "Call to initModules() failed")); } fail: SG_NULLFREE(pCtx, psz_js); SG_PATHNAME_NULLFREE(pCtx, pModuleDirPath); SG_RBTREE_ITERATOR_NULLFREE(pCtx, pModuleDir); SG_RBTREE_NULLFREE(pCtx, pModules); }