FRAGMENT(JSObject, simple) { JS::Rooted<JSObject*> glob(cx, JS::CurrentGlobalOrNull(cx)); JS::Rooted<JSObject*> plain(cx, JS_NewPlainObject(cx)); JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx)); JS::Rooted<JSObject*> func(cx, (JSObject*) JS_NewFunction(cx, (JSNative) 1, 0, 0, global, "dys")); JS::Rooted<JSObject*> anon(cx, (JSObject*) JS_NewFunction(cx, (JSNative) 1, 0, 0, global, 0)); JS::Rooted<JSFunction*> funcPtr(cx, JS_NewFunction(cx, (JSNative) 1, 0, 0, global, "formFollows")); JSObject& plainRef = *plain; JSFunction& funcRef = *funcPtr; JSObject* plainRaw = plain; JSObject* funcRaw = func; breakpoint(); (void) glob; (void) plain; (void) func; (void) anon; (void) funcPtr; (void) &plainRef; (void) &funcRef; (void) plainRaw; (void) funcRaw; }
NS_IMETHODIMP nsJetpack::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext * cx, JSObject * obj, jsval id, PRUint32 flags, JSObject * *objp, PRBool *_retval) { if (JSVAL_IS_STRING(id) && strncmp(JS_GetStringBytes(JSVAL_TO_STRING(id)), "get", 3) == 0) { JSFunction *get = JS_NewFunction(cx, getEndpoint, 0, 0, JS_GetParent(cx, obj), "get"); if (!get) { JS_ReportOutOfMemory(cx); *_retval = PR_FALSE; return NS_OK; } JSObject *getObj = JS_GetFunctionObject(get); jsid idid; *objp = obj; *_retval = (JS_ValueToId(cx, id, &idid) && JS_DefinePropertyById(cx, obj, idid, OBJECT_TO_JSVAL(getObj), nsnull, nsnull, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)); return NS_OK; } *objp = nsnull; *_retval = PR_TRUE; return NS_OK; }
void Promise::CreateWrapper(JS::Handle<JSObject*> aDesiredProto, ErrorResult& aRv) { AutoJSAPI jsapi; if (!jsapi.Init(mGlobal)) { aRv.Throw(NS_ERROR_UNEXPECTED); return; } JSContext* cx = jsapi.cx(); JSFunction* doNothingFunc = JS_NewFunction(cx, DoNothingPromiseExecutor, /* nargs = */ 2, /* flags = */ 0, nullptr); if (!doNothingFunc) { JS_ClearPendingException(cx); aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } JS::Rooted<JSObject*> doNothingObj(cx, JS_GetFunctionObject(doNothingFunc)); mPromiseObj = JS::NewPromiseObject(cx, doNothingObj, aDesiredProto); if (!mPromiseObj) { JS_ClearPendingException(cx); aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } }
static JSObject * GetScopeFunction(JSContext *cx, JSObject *outerObj) { jsval v; if (!JS_GetReservedSlot(cx, outerObj, sScopeFunSlot, &v)) { return nsnull; } JSObject *unsafeObj = GetUnsafeObject(outerObj); JSObject *scopeobj = JS_GetGlobalForObject(cx, unsafeObj); OBJ_TO_INNER_OBJECT(cx, scopeobj); if (!scopeobj) { return nsnull; } if (JSVAL_IS_OBJECT(v)) { JSObject *funobj = JSVAL_TO_OBJECT(v); if (JS_GetGlobalForObject(cx, funobj) == scopeobj) { return funobj; } } JSFunction *fun = JS_NewFunction(cx, DummyNative, 0, 0, scopeobj, "SJOWContentBoundary"); if (!fun) { return nsnull; } JSObject *funobj = JS_GetFunctionObject(fun); if (!JS_SetReservedSlot(cx, outerObj, sScopeFunSlot, OBJECT_TO_JSVAL(funobj))) { return nsnull; } return funobj; }
static JSObject * GeneratePropertyOp(JSContext *cx, JSObject *obj, jsval idval, uintN argc, const char *name, JSPropertyOp pop) { // The JS engine provides two reserved slots on function objects for // XPConnect to use. Use them to stick the necessary info here. JSFunction *fun = JS_NewFunction(cx, reinterpret_cast<JSNative>(PropertyOpForwarder), argc, JSFUN_FAST_NATIVE, obj, name); if(!fun) return JS_FALSE; JSObject *funobj = JS_GetFunctionObject(fun); JSAutoTempValueRooter tvr(cx, OBJECT_TO_JSVAL(funobj)); // Unfortunately, we cannot guarantee that JSPropertyOp is aligned. Use a // second object to work around this. JSObject *ptrobj = JS_NewObject(cx, &PointerHolderClass, nsnull, funobj); if(!ptrobj) return JS_FALSE; JSPropertyOp *popp = new JSPropertyOp; if(!popp) return JS_FALSE; *popp = pop; JS_SetPrivate(cx, ptrobj, popp); JS_SetReservedSlot(cx, funobj, 0, OBJECT_TO_JSVAL(ptrobj)); JS_SetReservedSlot(cx, funobj, 1, idval); return funobj; }
JSBool XPC_COW_WrapFunction(JSContext *cx, JSObject *outerObj, JSObject *funobj, jsval *rval) { jsval funobjVal = OBJECT_TO_JSVAL(funobj); JSFunction *wrappedFun = reinterpret_cast<JSFunction *>(xpc_GetJSPrivate(funobj)); JSNative native = JS_GetFunctionNative(cx, wrappedFun); if (!native || native == XPC_COW_FunctionWrapper) { *rval = funobjVal; return JS_TRUE; } JSFunction *funWrapper = JS_NewFunction(cx, XPC_COW_FunctionWrapper, JS_GetFunctionArity(wrappedFun), 0, JS_GetGlobalForObject(cx, outerObj), JS_GetFunctionName(wrappedFun)); if (!funWrapper) { return JS_FALSE; } JSObject *funWrapperObj = JS_GetFunctionObject(funWrapper); *rval = OBJECT_TO_JSVAL(funWrapperObj); return JS_SetReservedSlot(cx, funWrapperObj, XPCWrapper::eWrappedFunctionSlot, funobjVal); }
bool rs::jsapi::Global::DefineFunction(Context& cx, const char* name, FunctionCallback callback, unsigned attrs) { JSAutoRequest ar(cx); JS::RootedFunction func(cx, JS_NewFunction(cx, CallFunction, 0, 0, JS::NullPtr(), name)); JS::RootedObject funcObj(cx, JS_GetFunctionObject(func)); JS::RootedObject privateFuncObj(cx, JS_NewObject(cx, &privateFunctionStateClass_, JS::NullPtr())); JS_SetPrivate(privateFuncObj, new GlobalFunctionState(callback)); JS_DefineProperty(cx, funcObj, privateFunctionStatePropertyName_, privateFuncObj, 0, nullptr, nullptr); return JS_DefineProperty(cx, cx.getGlobal(), name, funcObj, attrs, nullptr, nullptr); }
DEFINE_CONSTRUCTOR() { JL_ASSERT_CONSTRUCTING(); JL_DEFINE_CONSTRUCTOR_OBJ; JSFunction *allocFunction; allocFunction = JS_NewFunction(cx, _alloc, 0, 0, NULL, "alloc"); JL_ASSERT_ALLOC( allocFunction ); // "Unable to create allocation function." JS::RootedObject functionObject(cx, JS_GetFunctionObject(allocFunction)); JL_CHK( JL_SetReservedSlot( obj, SLOT_FUNCTION_ALLOC, OBJECT_TO_JSVAL(functionObject)) ); return true; JL_BAD; }
JSBool edjs_init(JSContext *cx, JSObject **ret) { JSBool ok = JS_TRUE; JSFunction *func = NULL; func = JS_NewFunction(cx, edjsdb_Construct, 0, 0, NULL, NULL); *ret = (JSObject *)func;//JS_GetFunctionObject(func); goto finish; error: ok = JS_FALSE; finish: return ok; }
FRAGMENT(JSObject, simple) { AutoSuppressHazardsForTest noanalysis; JS::Rooted<JSObject*> glob(cx, JS::CurrentGlobalOrNull(cx)); JS::Rooted<JSObject*> plain(cx, JS_NewPlainObject(cx)); JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx)); JS::Rooted<JSObject*> func(cx, (JSObject*) JS_NewFunction(cx, (JSNative) 1, 0, 0, "dys")); JS::Rooted<JSObject*> anon(cx, (JSObject*) JS_NewFunction(cx, (JSNative) 1, 0, 0, nullptr)); JS::Rooted<JSFunction*> funcPtr(cx, JS_NewFunction(cx, (JSNative) 1, 0, 0, "formFollows")); JSObject& plainRef = *plain; JSFunction& funcRef = *funcPtr; JSObject* plainRaw = plain; JSObject* funcRaw = func; // JS_NewObject will now assert if you feed it a bad class name, so mangle // the name after construction. char namebuf[20] = "goodname"; static JSClass cls { namebuf }; JS::RootedObject badClassName(cx, JS_NewObject(cx, &cls)); strcpy(namebuf, "\xc7X"); breakpoint(); use(glob); use(plain); use(func); use(anon); use(funcPtr); use(&plainRef); use(&funcRef); use(plainRaw); use(funcRaw); }
ScriptValuePtr TraceMonkeyEngine::createFunction(NativeFunction func, int numArgs) { JSFunction* jsFunc = JS_NewFunction(context, (JSNative)func, numArgs, 0, NULL, NULL); bool success = JS_AddNamedRoot(context, &jsFunc, "TraceMonkeyEngine::createFunction temp val");; // Ensure our value won't be GCed assert(success); assert(jsFunc != NULL); assert(JSVAL_IS_OBJECT(OBJECT_TO_JSVAL(jsFunc))); ScriptValuePtr ret( new TraceMonkeyValue( this, true, OBJECT_TO_JSVAL(jsFunc) ) ); success = JS_RemoveRoot(context, &jsFunc); assert(success); return ret; }
void register_cocos2dx_js_extensions(JSContext* cx, JSObject* global) { // first, try to get the ns jsval nsval; JSObject *ns; JS_GetProperty(cx, global, "cc", &nsval); if (nsval == JSVAL_VOID) { ns = JS_NewObject(cx, NULL, NULL, NULL); nsval = OBJECT_TO_JSVAL(ns); JS_SetProperty(cx, global, "cc", &nsval); } else { JS_ValueToObject(cx, nsval, &ns); } JS_DefineFunction(cx, global, "__associateObjWithNative", js_cocos2dx_swap_native_object, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "__getPlatform", js_platform, 0, JSPROP_READONLY | JSPROP_PERMANENT); JSObject *tmpObj; JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "getChildren", js_cocos2dx_CCNode_getChildren, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCAction_prototype, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCAction_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCAction_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCAnimation_prototype, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCAnimation_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCAnimation_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCSpriteFrame_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCSpriteFrame_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, js_cocos2dx_CCMenuItem_prototype, "setCallback", js_cocos2dx_setCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Node.prototype; })()")); JS_DefineFunction(cx, tmpObj, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Menu; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenu_create, 0, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItem; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItem_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemSprite; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemSprite_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemImage; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemImage_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemLabel; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemLabel_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemAtlasFont; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemAtlasFont_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemFont; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemFont_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemToggle; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemToggle_create, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Sequence; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCSequence_create, 0, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Spawn; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCSpawn_create, 0, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Animation; })()")); JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCAnimation_create, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, ns, "registerTargettedDelegate", js_cocos2dx_JSTouchDelegate_registerTargettedDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, ns, "registerStandardDelegate", js_cocos2dx_JSTouchDelegate_registerStandardDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.CallFunc; })()")); JS_DefineFunction(cx, tmpObj, "create", js_callFunc, 1, JSPROP_READONLY | JSPROP_PERMANENT); tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return this; })()")); JS_DefineFunction(cx, tmpObj, "garbageCollect", js_forceGC, 1, JSPROP_READONLY | JSPROP_PERMANENT); // add constructor for CCSet JSFunction *ccSetConstructor = JS_NewFunction(cx, js_cocos2dx_CCSet_constructor, 0, JSPROP_READONLY | JSPROP_PERMANENT, NULL, "constructor"); JSObject *ctor = JS_GetFunctionObject(ccSetConstructor); JS_LinkConstructorAndPrototype(cx, ctor, js_cocos2dx_CCSet_prototype); }
JSBool XPCNativeMember::Resolve(XPCCallContext& ccx, XPCNativeInterface* iface) { if(IsConstant()) { const nsXPTConstant* constant; if(NS_FAILED(iface->GetInterfaceInfo()->GetConstant(mIndex, &constant))) return JS_FALSE; const nsXPTCMiniVariant& mv = *constant->GetValue(); // XXX Big Hack! nsXPTCVariant v; v.flags = 0; v.type = constant->GetType(); memcpy(&v.val, &mv.val, sizeof(mv.val)); jsval resultVal; if(!XPCConvert::NativeData2JS(ccx, &resultVal, &v.val, v.type, nsnull, nsnull, nsnull)) return JS_FALSE; { // scoped lock XPCAutoLock lock(ccx.GetRuntime()->GetMapLock()); mVal = resultVal; mFlags |= RESOLVED; } return JS_TRUE; } // else... // This is a method or attribute - we'll be needing a function object intN argc; intN flags; JSNative callback; if(IsMethod()) { const nsXPTMethodInfo* info; if(NS_FAILED(iface->GetInterfaceInfo()->GetMethodInfo(mIndex, &info))) return JS_FALSE; // Note: ASSUMES that retval is last arg. argc = (intN) info->GetParamCount(); if(argc && info->GetParam((uint8)(argc-1)).IsRetval()) argc-- ; flags = 0; callback = XPC_WN_CallMethod; } else { if(IsWritableAttribute()) flags = JSFUN_GETTER | JSFUN_SETTER; else flags = JSFUN_GETTER; argc = 0; callback = XPC_WN_GetterSetter; } // We need to use the safe context for this thread because we don't want // to parent the new (and cached forever!) function object to the current // JSContext's global object. That would be bad! JSContext* cx = ccx.GetSafeJSContext(); if(!cx) return JS_FALSE; const char *memberName = iface->GetMemberName(ccx, this); jsrefcount suspendDepth = 0; if(cx != ccx) { // Switching contexts, suspend the old and enter the new request. suspendDepth = JS_SuspendRequest(ccx); JS_BeginRequest(cx); } JSFunction *fun = JS_NewFunction(cx, callback, argc, flags, nsnull, memberName); if(suspendDepth) { JS_EndRequest(cx); JS_ResumeRequest(ccx, suspendDepth); } if(!fun) return JS_FALSE; JSObject* funobj = JS_GetFunctionObject(fun); if(!funobj) return JS_FALSE; AUTO_MARK_JSVAL(ccx, OBJECT_TO_JSVAL(funobj)); STOBJ_CLEAR_PARENT(funobj); STOBJ_CLEAR_PROTO(funobj); if(!JS_SetReservedSlot(ccx, funobj, 0, PRIVATE_TO_JSVAL(iface))|| !JS_SetReservedSlot(ccx, funobj, 1, PRIVATE_TO_JSVAL(this))) return JS_FALSE; { // scoped lock XPCAutoLock lock(ccx.GetRuntime()->GetMapLock()); mVal = OBJECT_TO_JSVAL(funobj); mFlags |= RESOLVED; } return JS_TRUE; }
JavaClass_getPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp) { jsval idval; jclass java_class; const char *member_name; JavaClassDescriptor *class_descriptor; JavaMemberDescriptor *member_descriptor; JNIEnv *jEnv; JSJavaThreadState *jsj_env; JSBool result; /* printf("In JavaClass_getProperty\n"); */ /* Get the Java per-thread environment pointer for this JSContext */ jsj_env = jsj_EnterJava(cx, &jEnv); if (!jEnv) return JS_FALSE; if (!lookup_static_member_by_id(cx, jEnv, obj, &class_descriptor, id, &member_descriptor)) { jsj_ExitJava(jsj_env); return JS_FALSE; } if (!member_descriptor) { *vp = JSVAL_VOID; jsj_ExitJava(jsj_env); return JS_TRUE; } java_class = class_descriptor->java_class; if (member_descriptor->field) { if (!member_descriptor->methods) { result = jsj_GetJavaFieldValue(cx, jEnv, member_descriptor->field, java_class, vp); jsj_ExitJava(jsj_env); return result; } else { JS_ASSERT(0); } } else { JSFunction *function; /* TODO - eliminate JSFUN_BOUND_METHOD */ if (member_descriptor->methods->is_alias) { /* If this is an explicit resolution of an overloaded method, use the fully-qualified method name as the name of the resulting JS function, i.e. "myMethod(int,long)" */ JS_IdToValue(cx, id, &idval); member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); } else { /* Either not explicit resolution of overloaded method or explicit resolution was unnecessary because method was not overloaded. */ member_name = member_descriptor->name; } function = JS_NewFunction(cx, jsj_JavaStaticMethodWrapper, 0, JSFUN_BOUND_METHOD, obj, member_name); if (!function) { jsj_ExitJava(jsj_env); return JS_FALSE; } *vp = OBJECT_TO_JSVAL(JS_GetFunctionObject(function)); } jsj_ExitJava(jsj_env); return JS_TRUE; }
JSBool ncurses_initialize (JSContext* cx) { JSObject* object = JS_DefineObject( cx, JS_GetGlobalObject(cx), ncurses_class.name, &ncurses_class, NULL, JSPROP_PERMANENT|JSPROP_READONLY|JSPROP_ENUMERATE ); if (object) { JS_DefineFunctions(cx, object, ncurses_methods); jsval property; JSObject* Buffering = JS_NewObject(cx, NULL, NULL, NULL); jsval jsBuffering = OBJECT_TO_JSVAL(Buffering); JS_SetProperty(cx, object, "Buffering", &jsBuffering); property = INT_TO_JSVAL(1); JS_SetProperty(cx, Buffering, "Normal", &property); property = INT_TO_JSVAL(2); JS_SetProperty(cx, Buffering, "Raw", &property); property = INT_TO_JSVAL(3); JS_SetProperty(cx, Buffering, "CBreak", &property); JSObject* Colors = JS_NewObject(cx, NULL, NULL, NULL); jsval jsColors = OBJECT_TO_JSVAL(Colors); JS_SetProperty(cx, object, "Colors", &jsColors); property = INT_TO_JSVAL(-1); JS_SetProperty(cx, Colors, "Normal", &property); property = INT_TO_JSVAL(COLOR_BLACK); JS_SetProperty(cx, Colors, "Black", &property); property = INT_TO_JSVAL(COLOR_RED); JS_SetProperty(cx, Colors, "Red", &property); property = INT_TO_JSVAL(COLOR_GREEN); JS_SetProperty(cx, Colors, "Green", &property); property = INT_TO_JSVAL(COLOR_YELLOW); JS_SetProperty(cx, Colors, "Yellow", &property); property = INT_TO_JSVAL(COLOR_BLUE); JS_SetProperty(cx, Colors, "Blue", &property); property = INT_TO_JSVAL(COLOR_MAGENTA); JS_SetProperty(cx, Colors, "Magenta", &property); property = INT_TO_JSVAL(COLOR_CYAN); JS_SetProperty(cx, Colors, "Cyan", &property); property = INT_TO_JSVAL(COLOR_WHITE); JS_SetProperty(cx, Colors, "White", &property); JSObject* Attributes = JS_NewObject(cx, NULL, NULL, NULL); jsval jsAttributes = OBJECT_TO_JSVAL(Attributes); JS_SetProperty(cx, object, "Attributes", &jsAttributes); property = INT_TO_JSVAL(A_NORMAL); // Normal display (no highlight) JS_SetProperty(cx, Attributes, "Normal", &property); property = INT_TO_JSVAL(A_BLINK); // Blinking JS_SetProperty(cx, Attributes, "Blink", &property); property = INT_TO_JSVAL(A_BOLD); // Extra bright or bold JS_SetProperty(cx, Attributes, "Bold", &property); property = INT_TO_JSVAL(A_DIM); // Half bright JS_SetProperty(cx, Attributes, "Dim", &property); property = INT_TO_JSVAL(A_REVERSE); // Reverse video JS_SetProperty(cx, Attributes, "Reverse", &property); property = INT_TO_JSVAL(A_STANDOUT); // Best highlighting mode of the terminal. JS_SetProperty(cx, Attributes, "Standout", &property); property = INT_TO_JSVAL(A_UNDERLINE); // Underlining JS_SetProperty(cx, Attributes, "Underline", &property); property = INT_TO_JSVAL(A_PROTECT); // Protected mode JS_SetProperty(cx, Attributes, "Protect", &property); property = INT_TO_JSVAL(A_INVIS); JS_SetProperty(cx, Attributes, "Invisible", &property); JSObject* Cursor = JS_NewObject(cx, NULL, NULL, NULL); jsval jsCursor = OBJECT_TO_JSVAL(Cursor); JS_SetProperty(cx, object, "Cursor", &jsCursor); property = INT_TO_JSVAL(0); JS_SetProperty(cx, Cursor, "Invisible", &property); property = INT_TO_JSVAL(1); JS_SetProperty(cx, Cursor, "Normal", &property); property = INT_TO_JSVAL(2); JS_SetProperty(cx, Cursor, "Visible", &property); JSObject* Keys = JS_NewObject(cx, NULL, NULL, NULL); jsval jsKeys = OBJECT_TO_JSVAL(Keys); JS_SetProperty(cx, object, "Keys", &jsKeys); property = INT_TO_JSVAL(KEY_BREAK); JS_SetProperty(cx, Keys, "Break", &property); property = INT_TO_JSVAL(KEY_DOWN); JS_SetProperty(cx, Keys, "Down", &property); property = INT_TO_JSVAL(KEY_UP); JS_SetProperty(cx, Keys, "Up", &property); property = INT_TO_JSVAL(KEY_LEFT); JS_SetProperty(cx, Keys, "Left", &property); property = INT_TO_JSVAL(KEY_RIGHT); JS_SetProperty(cx, Keys, "Right", &property); property = INT_TO_JSVAL(KEY_HOME); JS_SetProperty(cx, Keys, "Home", &property); property = INT_TO_JSVAL(KEY_BACKSPACE); JS_SetProperty(cx, Keys, "Backspace", &property); property = OBJECT_TO_JSVAL(JS_GetFunctionObject( JS_NewFunction(cx, ncurses_KEY_F, 1, 0, NULL, "KEY_F") )); JS_SetProperty(cx, Keys, "F", &property); property = INT_TO_JSVAL(KEY_DL); JS_SetProperty(cx, Keys, "DeleteLine", &property); property = INT_TO_JSVAL(KEY_IL); JS_SetProperty(cx, Keys, "InsertLine", &property); property = INT_TO_JSVAL(KEY_DC); JS_SetProperty(cx, Keys, "DeleteChar", &property); property = INT_TO_JSVAL(KEY_IC); JS_SetProperty(cx, Keys, "EnterInsertMode", &property); property = INT_TO_JSVAL(KEY_EIC); JS_SetProperty(cx, Keys, "EndInsertMode", &property); property = INT_TO_JSVAL(KEY_CLEAR); JS_SetProperty(cx, Keys, "Clear", &property); property = INT_TO_JSVAL(KEY_EOS); JS_SetProperty(cx, Keys, "EndOfScreen", &property); property = INT_TO_JSVAL(KEY_EOL); JS_SetProperty(cx, Keys, "EndOfLine", &property); property = INT_TO_JSVAL(KEY_SF); JS_SetProperty(cx, Keys, "ScrollForward", &property); property = INT_TO_JSVAL(KEY_SR); JS_SetProperty(cx, Keys, "ScrollBackward", &property); property = INT_TO_JSVAL(KEY_NPAGE); JS_SetProperty(cx, Keys, "NextPage", &property); property = INT_TO_JSVAL(KEY_PPAGE); JS_SetProperty(cx, Keys, "PreviousPage", &property); property = INT_TO_JSVAL(KEY_STAB); JS_SetProperty(cx, Keys, "SetTab", &property); property = INT_TO_JSVAL(KEY_CTAB); JS_SetProperty(cx, Keys, "ClearTab", &property); property = INT_TO_JSVAL(KEY_CATAB); JS_SetProperty(cx, Keys, "ClearAllTabs", &property); property = INT_TO_JSVAL(KEY_ENTER); JS_SetProperty(cx, Keys, "Enter", &property); property = INT_TO_JSVAL(KEY_SRESET); JS_SetProperty(cx, Keys, "SoftReset", &property); property = INT_TO_JSVAL(KEY_RESET); JS_SetProperty(cx, Keys, "Reset", &property); property = INT_TO_JSVAL(KEY_PRINT); JS_SetProperty(cx, Keys, "Print", &property); property = INT_TO_JSVAL(KEY_LL); JS_SetProperty(cx, Keys, "Bottom", &property); property = INT_TO_JSVAL(KEY_A1); JS_SetProperty(cx, Keys, "UpperLeft", &property); property = INT_TO_JSVAL(KEY_A3); JS_SetProperty(cx, Keys, "UpperRight", &property); property = INT_TO_JSVAL(KEY_B2); JS_SetProperty(cx, Keys, "Center", &property); property = INT_TO_JSVAL(KEY_C1); JS_SetProperty(cx, Keys, "LowerLeft", &property); property = INT_TO_JSVAL(KEY_C3); JS_SetProperty(cx, Keys, "LowerRight", &property); property = INT_TO_JSVAL(KEY_BTAB); JS_SetProperty(cx, Keys, "BackTab", &property); property = INT_TO_JSVAL(KEY_BEG); JS_SetProperty(cx, Keys, "Beginning", &property); property = INT_TO_JSVAL(KEY_CANCEL); JS_SetProperty(cx, Keys, "Cancel", &property); property = INT_TO_JSVAL(KEY_CLOSE); JS_SetProperty(cx, Keys, "Close", &property); property = INT_TO_JSVAL(KEY_COMMAND); JS_SetProperty(cx, Keys, "Command", &property); property = INT_TO_JSVAL(KEY_COPY); JS_SetProperty(cx, Keys, "Copy", &property); property = INT_TO_JSVAL(KEY_CREATE); JS_SetProperty(cx, Keys, "Create", &property); property = INT_TO_JSVAL(KEY_END); JS_SetProperty(cx, Keys, "End", &property); property = INT_TO_JSVAL(KEY_EXIT); JS_SetProperty(cx, Keys, "Exit", &property); property = INT_TO_JSVAL(KEY_FIND); JS_SetProperty(cx, Keys, "Find", &property); property = INT_TO_JSVAL(KEY_HELP); JS_SetProperty(cx, Keys, "Help", &property); property = INT_TO_JSVAL(KEY_MARK); JS_SetProperty(cx, Keys, "Mark", &property); property = INT_TO_JSVAL(KEY_MESSAGE); JS_SetProperty(cx, Keys, "Message", &property); property = INT_TO_JSVAL(KEY_MOUSE); JS_SetProperty(cx, Keys, "Mouse", &property); property = INT_TO_JSVAL(KEY_MOVE); JS_SetProperty(cx, Keys, "Move", &property); property = INT_TO_JSVAL(KEY_NEXT); JS_SetProperty(cx, Keys, "Next", &property); property = INT_TO_JSVAL(KEY_OPEN); JS_SetProperty(cx, Keys, "Open", &property); property = INT_TO_JSVAL(KEY_OPTIONS); JS_SetProperty(cx, Keys, "Options", &property); property = INT_TO_JSVAL(KEY_PREVIOUS); JS_SetProperty(cx, Keys, "Previous", &property); property = INT_TO_JSVAL(KEY_REDO); JS_SetProperty(cx, Keys, "Redo", &property); property = INT_TO_JSVAL(KEY_REFERENCE); JS_SetProperty(cx, Keys, "Reference", &property); property = INT_TO_JSVAL(KEY_REFRESH); JS_SetProperty(cx, Keys, "Refresh", &property); property = INT_TO_JSVAL(KEY_REPLACE); JS_SetProperty(cx, Keys, "Replace", &property); property = INT_TO_JSVAL(KEY_RESIZE); JS_SetProperty(cx, Keys, "Resize", &property); property = INT_TO_JSVAL(KEY_RESTART); JS_SetProperty(cx, Keys, "Restart", &property); property = INT_TO_JSVAL(KEY_RESUME); JS_SetProperty(cx, Keys, "Resume", &property); property = INT_TO_JSVAL(KEY_SAVE); JS_SetProperty(cx, Keys, "Save", &property); property = INT_TO_JSVAL(KEY_SELECT); JS_SetProperty(cx, Keys, "Select", &property); property = INT_TO_JSVAL(KEY_SUSPEND); JS_SetProperty(cx, Keys, "Suspend", &property); property = INT_TO_JSVAL(KEY_UNDO); JS_SetProperty(cx, Keys, "Undo", &property); JSObject* Shifted = JS_NewObject(cx, NULL, NULL, NULL); jsval jsShifted = OBJECT_TO_JSVAL(Shifted); JS_SetProperty(cx, Keys, "Shifted", &jsShifted); property = INT_TO_JSVAL(KEY_SBEG); JS_SetProperty(cx, Shifted, "Beginning", &property); property = INT_TO_JSVAL(KEY_SCANCEL); JS_SetProperty(cx, Shifted, "Cancel", &property); property = INT_TO_JSVAL(KEY_SCOMMAND); JS_SetProperty(cx, Shifted, "Command", &property); property = INT_TO_JSVAL(KEY_SCOPY); JS_SetProperty(cx, Shifted, "Copy", &property); property = INT_TO_JSVAL(KEY_SCREATE); JS_SetProperty(cx, Shifted, "Create", &property); property = INT_TO_JSVAL(KEY_SDC); JS_SetProperty(cx, Shifted, "DeleteChar", &property); property = INT_TO_JSVAL(KEY_SDL); JS_SetProperty(cx, Shifted, "DeleteLine", &property); property = INT_TO_JSVAL(KEY_SEND); JS_SetProperty(cx, Shifted, "End", &property); property = INT_TO_JSVAL(KEY_SEOL); JS_SetProperty(cx, Shifted, "EndOfLine", &property); property = INT_TO_JSVAL(KEY_SEXIT); JS_SetProperty(cx, Shifted, "Exit", &property); property = INT_TO_JSVAL(KEY_SFIND); JS_SetProperty(cx, Shifted, "Find", &property); property = INT_TO_JSVAL(KEY_SHELP); JS_SetProperty(cx, Shifted, "Help", &property); property = INT_TO_JSVAL(KEY_SHOME); JS_SetProperty(cx, Shifted, "Home", &property); property = INT_TO_JSVAL(KEY_SIC); JS_SetProperty(cx, Shifted, "EnterInsertMode", &property); property = INT_TO_JSVAL(KEY_SLEFT); JS_SetProperty(cx, Shifted, "Left", &property); property = INT_TO_JSVAL(KEY_SMESSAGE); JS_SetProperty(cx, Shifted, "Message", &property); property = INT_TO_JSVAL(KEY_SMOVE); JS_SetProperty(cx, Shifted, "Move", &property); property = INT_TO_JSVAL(KEY_SNEXT); JS_SetProperty(cx, Shifted, "Next", &property); property = INT_TO_JSVAL(KEY_SOPTIONS); JS_SetProperty(cx, Shifted, "Options", &property); property = INT_TO_JSVAL(KEY_SPREVIOUS); JS_SetProperty(cx, Shifted, "Previous", &property); property = INT_TO_JSVAL(KEY_SPRINT); JS_SetProperty(cx, Shifted, "Print", &property); property = INT_TO_JSVAL(KEY_SREDO); JS_SetProperty(cx, Shifted, "Redo", &property); property = INT_TO_JSVAL(KEY_SREPLACE); JS_SetProperty(cx, Shifted, "Replace", &property); property = INT_TO_JSVAL(KEY_SRIGHT); JS_SetProperty(cx, Shifted, "Right", &property); property = INT_TO_JSVAL(KEY_SRSUME); JS_SetProperty(cx, Shifted, "Resume", &property); property = INT_TO_JSVAL(KEY_SSAVE); JS_SetProperty(cx, Shifted, "Save", &property); property = INT_TO_JSVAL(KEY_SSUSPEND); JS_SetProperty(cx, Shifted, "Suspend", &property); property = INT_TO_JSVAL(KEY_SUNDO); JS_SetProperty(cx, Shifted, "Undo", &property); return JS_TRUE; } return JS_FALSE; }
JSBool XPCDispInterface::Member::GetValue(XPCCallContext& ccx, XPCNativeInterface * iface, jsval * retval) const { // This is a method or attribute - we'll be needing a function object // We need to use the safe context for this thread because we don't want // to parent the new (and cached forever!) function object to the current // JSContext's global object. That would be bad! if((mType & RESOLVED) == 0) { JSContext* cx = ccx.GetSafeJSContext(); if(!cx) return JS_FALSE; intN argc; intN flags; JSNative callback; // Is this a function or a parameterized getter/setter if(IsFunction() || IsParameterizedProperty()) { argc = GetParamCount(); flags = 0; callback = XPC_IDispatch_CallMethod; } else { if(IsSetter()) { flags = JSFUN_GETTER | JSFUN_SETTER; } else { flags = JSFUN_GETTER; } argc = 0; callback = XPC_IDispatch_GetterSetter; } #ifdef JAXER JSAutoRequest ar(cx); #endif JSFunction *fun = JS_NewFunction(cx, callback, argc, flags, nsnull, JS_GetStringBytes(JSVAL_TO_STRING(mName))); if(!fun) return JS_FALSE; JSObject* funobj = JS_GetFunctionObject(fun); if(!funobj) return JS_FALSE; // Store ourselves and our native interface within the JSObject if(!JS_SetReservedSlot(ccx, funobj, 0, PRIVATE_TO_JSVAL(this))) return JS_FALSE; if(!JS_SetReservedSlot(ccx, funobj, 1, PRIVATE_TO_JSVAL(iface))) return JS_FALSE; { // scoped lock XPCAutoLock lock(ccx.GetRuntime()->GetMapLock()); const_cast<Member*>(this)->mVal = OBJECT_TO_JSVAL(funobj); const_cast<Member*>(this)->mType |= RESOLVED; } } *retval = mVal; return JS_TRUE; }
static JSObject* CreateInterfaceObject(JSContext* cx, JSObject* global, JSObject* receiver, JSClass* constructorClass, JSNative constructorNative, unsigned ctorNargs, JSObject* proto, Prefable<JSFunctionSpec>* staticMethods, Prefable<ConstantSpec>* constants, const char* name) { JSObject* constructor; if (constructorClass) { JSObject* functionProto = JS_GetFunctionPrototype(cx, global); if (!functionProto) { return NULL; } constructor = JS_NewObject(cx, constructorClass, functionProto, global); } else { MOZ_ASSERT(constructorNative); JSFunction* fun = JS_NewFunction(cx, constructorNative, ctorNargs, JSFUN_CONSTRUCTOR, global, name); if (!fun) { return NULL; } constructor = JS_GetFunctionObject(fun); } if (!constructor) { return NULL; } if (staticMethods && !DefinePrefable(cx, constructor, staticMethods)) { return NULL; } if (constructorClass) { JSFunction* toString = js::DefineFunctionWithReserved(cx, constructor, "toString", InterfaceObjectToString, 0, 0); if (!toString) { return NULL; } JSObject* toStringObj = JS_GetFunctionObject(toString); js::SetFunctionNativeReserved(toStringObj, TOSTRING_CLASS_RESERVED_SLOT, PRIVATE_TO_JSVAL(constructorClass)); JSString *str = ::JS_InternString(cx, name); if (!str) { return NULL; } js::SetFunctionNativeReserved(toStringObj, TOSTRING_NAME_RESERVED_SLOT, STRING_TO_JSVAL(str)); } if (constants && !DefinePrefable(cx, constructor, constants)) { return NULL; } if (proto && !JS_LinkConstructorAndPrototype(cx, constructor, proto)) { return NULL; } JSBool alreadyDefined; if (!JS_AlreadyHasOwnProperty(cx, receiver, name, &alreadyDefined)) { return NULL; } // This is Enumerable: False per spec. if (!alreadyDefined && !JS_DefineProperty(cx, receiver, name, OBJECT_TO_JSVAL(constructor), NULL, NULL, 0)) { return NULL; } return constructor; }