JSObject * js_InitNavigatorClass(JSContext *cx, JSObject *obj) { JSObject *Navigator; Navigator = JS_DefineObject(cx, obj, "navigator", &navigator_class, NULL, 0); if (!Navigator) return NULL; if (!JS_DefineFunctions(cx, Navigator, navigator_static_methods)) return NULL; if (!JS_DefineProperties(cx, Navigator, navigator_properties)) return NULL; CMNavigator *cmNav; cmNav = JS_malloc(cx, sizeof *cmNav); if (!cmNav) return NULL; memset(cmNav, 0 , sizeof *cmNav); cmNav->appVersion = JS_malloc(cx, strlen("Firefox/2")); cmNav->appVersion = strdup("Firefox/2"); JS_SetPrivate(cx, Navigator, cmNav); return Navigator; }
void lm_RegisterComponentProp(const char *comp, const char *targetName, uint8 retType, ETCompPropSetterFunc setter, ETCompPropGetterFunc getter) { JSContext *cx; JSObject *arrayObj, *obj; jsval val; char *type, *set, *get; if (!comp || !targetName || !(cx = lm_crippled_decoder->js_context)) return; arrayObj = lm_DefineComponents(lm_crippled_decoder); if (!arrayObj) return; if (!JS_GetProperty(cx, arrayObj, comp, &val) || !JSVAL_IS_OBJECT(val)) return; obj = JSVAL_TO_OBJECT(val); if (!JS_DefineProperty(cx, obj, targetName, JSVAL_VOID, component_mozilla_getProperty, component_mozilla_setProperty, 0)) { } type = JS_malloc(cx, XP_STRLEN(lm_typePrefix_str) + XP_STRLEN(targetName) + 1); if (type) { XP_STRCPY(type, lm_typePrefix_str); XP_STRCAT(type, targetName); if (!JS_DefineProperty(cx, obj, type, INT_TO_JSVAL((int32)retType), 0, 0, JSPROP_READONLY)) { } JS_free(cx, type); } get = JS_malloc(cx, XP_STRLEN(lm_getPrefix_str) + XP_STRLEN(targetName) + 1); if (get) { XP_STRCPY(get, lm_getPrefix_str); XP_STRCAT(get, targetName); if (!JS_DefineProperty(cx, obj, get, INT_TO_JSVAL(getter), 0, 0, JSPROP_READONLY)) { } JS_free(cx, get); } set = JS_malloc(cx, XP_STRLEN(lm_setPrefix_str) + XP_STRLEN(targetName) + 1); if (set) { XP_STRCPY(set, lm_setPrefix_str); XP_STRCAT(set, targetName); if (!JS_DefineProperty(cx, obj, set, INT_TO_JSVAL(setter), 0, 0, JSPROP_READONLY)) { } JS_free(cx, set); } }
JSObject* lm_DefineComponents(MochaDecoder *decoder) { JSObject *obj; JSComponentArray *array; JSContext *cx = decoder->js_context; JSPreDefComponent def_comps; JSComponent *component; jsint slot; obj = decoder->components; if (obj) return obj; array = JS_malloc(cx, sizeof(JSComponentArray)); if (!array) return NULL; XP_BZERO(array, sizeof *array); obj = JS_NewObject(cx, &lm_component_array_class, NULL, decoder->window_object); if (!obj || !JS_SetPrivate(cx, obj, array)) { JS_free(cx, array); return NULL; } if (!JS_DefineProperties(cx, obj, componentarray_props)) return NULL; array->decoder = HOLD_BACK_COUNT(decoder); array->obj = obj; /* Components can be added dynamically but some are predefined */ slot = 0; array->length = 0; def_comps = predef_components[slot]; while (def_comps.name) { component = JS_malloc(cx, sizeof(JSComponent)); if (!component) return NULL; if (ET_moz_VerifyComponentFunction(def_comps.func, &(component->active_callback), &(component->startup_callback))) { componentarray_create_component(cx, array, component, def_comps.name, array->length); } else { /*Component call failed somewhere.*/ JS_free(cx, component); } def_comps = predef_components[++slot]; } return obj; }
JS_SetTrap(JSContext *cx, JSScript *script, jsbytecode *pc, JSTrapHandler handler, void *closure) { JSRuntime *rt; JSTrap *trap; rt = cx->runtime; trap = FindTrap(rt, script, pc); if (trap) { /* Restore opcode at pc so it can be saved again. */ *pc = (jsbytecode)trap->op; } else { trap = (JSTrap *) JS_malloc(cx, sizeof *trap); if (!trap || !js_AddRoot(cx, &trap->closure, "trap->closure")) { if (trap) JS_free(cx, trap); return JS_FALSE; } } JS_APPEND_LINK(&trap->links, &rt->trapList); trap->script = script; trap->pc = pc; trap->op = (JSOp)*pc; trap->handler = handler; trap->closure = closure; *pc = JSOP_TRAP; return JS_TRUE; }
JSBool wimg_image_info_setter(JSContext * cx, JSObject * obj, jsval idval, jsval * vp) { JS_BeginRequest(cx); HANDLE hImage = JS_GetPrivate(cx, obj); JSString * newInfo = JS_ValueToString(cx, *vp); *vp = STRING_TO_JSVAL(newInfo); DWORD length = JS_GetStringLength(newInfo); LPWSTR chars = (LPWSTR)JS_GetStringChars(newInfo); if(*chars != 0xfeff) { length++; LPWSTR back = chars; chars = (LPWSTR)JS_malloc(cx, sizeof(WCHAR) * (length + 1)); memset(chars, 0, sizeof(WCHAR) * (length + 1)); *chars = 0xfeff; wcscat(chars, back); newInfo = JS_NewUCString(cx, (jschar*)chars, sizeof(WCHAR) * length); *vp = STRING_TO_JSVAL(newInfo); } DWORD errorCode = 0; if(!WIMSetImageInformation(hImage, (LPVOID)chars, length * sizeof(WCHAR))) errorCode = GetLastError(); JS_EndRequest(cx); return JS_TRUE; }
// static JSString* DOMProxyHandler::obj_toString(JSContext* cx, const char* className) { size_t nchars = sizeof("[object ]") - 1 + strlen(className); jschar* chars = static_cast<jschar*>(JS_malloc(cx, (nchars + 1) * sizeof(jschar))); if (!chars) { return NULL; } const char* prefix = "[object "; nchars = 0; while ((chars[nchars] = (jschar)*prefix) != 0) { nchars++, prefix++; } while ((chars[nchars] = (jschar)*className) != 0) { nchars++, className++; } chars[nchars++] = ']'; chars[nchars] = 0; JSString* str = JS_NewUCString(cx, chars, nchars); if (!str) { JS_free(cx, chars); } return str; }
static JSObject * reflect_anchor_array(MochaDecoder *decoder, JSClass *clasp, JSNative constructor, JSObject *document) { JSContext *cx; JSObject *obj, *prototype; JSObjectArray *array; JSDocument *doc; cx = decoder->js_context; doc = JS_GetPrivate(cx, document); if (!doc) return NULL; prototype = JS_InitClass(cx, decoder->window_object, NULL, clasp, constructor, 0, anchor_array_props, NULL, NULL, NULL); if (!prototype) return NULL; array = JS_malloc(cx, sizeof *array); if (!array) return NULL; XP_BZERO(array, sizeof *array); obj = JS_NewObject(cx, clasp, prototype, document); if (!obj || !JS_SetPrivate(cx, obj, array)) { JS_free(cx, array); return NULL; } array->decoder = HOLD_BACK_COUNT(decoder); array->layer_id = doc->layer_id; return obj; }
JS_SetTrap(JSContext *cx, JSScript *script, jsbytecode *pc, JSTrapHandler handler, void *closure) { JSRuntime *rt; JSTrap *trap; rt = cx->runtime; trap = FindTrap(rt, script, pc); if (trap) { JS_ASSERT(trap->script == script && trap->pc == pc); JS_ASSERT(*pc == JSOP_TRAP); } else { trap = (JSTrap *) JS_malloc(cx, sizeof *trap); if (!trap || !js_AddRoot(cx, &trap->closure, "trap->closure")) { if (trap) JS_free(cx, trap); return JS_FALSE; } JS_APPEND_LINK(&trap->links, &rt->trapList); trap->script = script; trap->pc = pc; trap->op = (JSOp)*pc; *pc = JSOP_TRAP; } trap->handler = handler; trap->closure = closure; return JS_TRUE; }
/** Throw an exception from our C code to be caught by the * running JavaScript program. * * To use this function, in most cases, do: * <pre> * if (badness) * return gpsee_throw(cx, "bad things just happened"); * </pre> * * @param cx Context from which to device runtime information * @param fmt printf-style format for the text to throw as an exception * @param ... printf-style arguments to fmt * * @returns JS_FALSE * * @warning This function will panic if the runtime cannot allocate * enough memory to prepare the exception-throwing message. * * @note This function uses the APR SureLynx gpsee_makeLogFormat() function, * so %m will work, and so will addPercentM_Handler() with surelynx. */ JSBool gpsee_throw(JSContext *cx, const char *fmt, ...) { char *message; va_list ap; size_t length; char fmtNew[GPSEE_MAX_LOG_MESSAGE_SIZE]; message = JS_malloc(cx, GPSEE_MAX_THROW_MESSAGE_SIZE); if (!message) panic(GPSEE_GLOBAL_NAMESPACE_NAME ": out of memory in gpsee_throw!"); va_start(ap, fmt); length = vsnprintf(message, GPSEE_MAX_THROW_MESSAGE_SIZE, gpsee_makeLogFormat(fmt, fmtNew), ap); va_end(ap); if (length <= 0) { gpsee_cpystrn(message, "JS Engine unnamed exception", GPSEE_MAX_THROW_MESSAGE_SIZE); length = strlen(message); } if (JS_IsExceptionPending(cx) == JS_TRUE) gpsee_log(cx, GLOG_ERR, GPSEE_GLOBAL_NAMESPACE_NAME ": Already throwing an exception; not throwing '%s'!", message); else JS_ReportError(cx, "%s", message); return JS_FALSE; }
JSBool win32_getenv(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JS_BeginRequest(cx); if(argc < 1) { JS_ReportError(cx, "getenv requires the name of the variable to be passed"); JS_EndRequest(cx); return JS_FALSE; } JSString * name = JS_ValueToString(cx, argv[0]); DWORD size = GetEnvironmentVariable((LPWSTR)JS_GetStringChars(name), NULL, 0); if(size == 0) { *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } LPWSTR valueBuffer = (LPWSTR)JS_malloc(cx, (size + 2) * sizeof(WCHAR)); size = GetEnvironmentVariable((LPWSTR)JS_GetStringChars(name), valueBuffer, size + 1); if(size == 0) { JS_free(cx, valueBuffer); *rval = JSVAL_FALSE; JS_EndRequest(cx); return JS_TRUE; } JSString * value = JS_NewUCString(cx, valueBuffer, size + 1); *rval = STRING_TO_JSVAL(value); JS_EndRequest(cx); return JS_TRUE; }
JSBool S_CCMenuItemLabel::jsitemWithLabel(JSContext *cx, uint32_t argc, jsval *vp) { JSObject* obj = (JSObject *)JS_THIS_OBJECT(cx, vp); S_CCRenderTexture* self = NULL; JSGET_PTRSHELL(S_CCRenderTexture, self, obj); if (self == NULL) return JS_FALSE; if (argc == 1) { JSObject *arg0; JS_ConvertArguments(cx, 1, JS_ARGV(cx, vp), "o", &arg0); CCNode* narg0; JSGET_PTRSHELL(CCNode, narg0, arg0); CCMenuItemLabel *ret = CCMenuItemLabel::itemWithLabel(narg0, self, menu_selector(S_CCMenuItemLabel::menuAction)); if (ret == NULL) { JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_TRUE; } do { JSObject *tmp = JS_NewObject(cx, S_CCMenuItemLabel::jsClass, S_CCMenuItemLabel::jsObject, NULL); pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t)); pt->flags = kPointerTemporary; pt->data = (void *)ret; JS_SetPrivate(tmp, pt); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(tmp)); } while(0); return JS_TRUE; } JS_SET_RVAL(cx, vp, JSVAL_TRUE); return JS_TRUE; }
static JSBool jsRead( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval ) { *rval = JSVAL_FALSE ; jsUsblpPoll_t *dev = (jsUsblpPoll_t *)JS_GetInstancePrivate( cx, obj, &jsUsblpClass_, NULL ); if( dev ) { unsigned numAvail = dev->bytesAvail(); if( 0 < numAvail ) { char *const inBuf = (char *)JS_malloc( cx, numAvail+1 ); unsigned numRead ; if( dev->read( inBuf, numAvail, numRead ) ) { JSString *sData = JS_NewString( cx, inBuf, numRead ); *rval = STRING_TO_JSVAL( sData ); } else { JS_ReportError( cx, "usblp read error\n" ); JS_free(cx, inBuf); } } } else JS_ReportError( cx, "Invalid usblp object\n" ); return JS_TRUE ; }
JSBool S_CCLabelTTF::jslabelWithString(JSContext *cx, uint32_t argc, jsval *vp) { if (argc == 5) { JSString *arg0; JSObject *arg1; int arg2; JSString *arg3; double arg4; JS_ConvertArguments(cx, 5, JS_ARGV(cx, vp), "SoiSd", &arg0, &arg1, &arg2, &arg3, &arg4); char *narg0 = JS_EncodeString(cx, arg0); CCSize* narg1; JSGET_PTRSHELL(CCSize, narg1, arg1); char *narg3 = JS_EncodeString(cx, arg3); CCLabelTTF *ret = CCLabelTTF::labelWithString(narg0, *narg1, (CCTextAlignment)arg2, narg3, arg4); if (ret == NULL) { JS_SET_RVAL(cx, vp, JSVAL_NULL); return JS_TRUE; } do { JSObject *tmp = JS_NewObject(cx, S_CCLabelTTF::jsClass, S_CCLabelTTF::jsObject, NULL); pointerShell_t *pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t)); pt->flags = kPointerTemporary; pt->data = (void *)ret; JS_SetPrivate(tmp, pt); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(tmp)); } while (0); return JS_TRUE; } JS_SET_RVAL(cx, vp, JSVAL_TRUE); return JS_TRUE; }
JS_XDRString(JSXDRState *xdr, JSString **strp) { uint32 nchars; jschar *chars; if (xdr->mode == JSXDR_ENCODE) nchars = JSSTRING_LENGTH(*strp); if (!JS_XDRUint32(xdr, &nchars)) return JS_FALSE; if (xdr->mode == JSXDR_DECODE) { chars = (jschar *) JS_malloc(xdr->cx, (nchars + 1) * sizeof(jschar)); if (!chars) return JS_FALSE; } else { chars = JSSTRING_CHARS(*strp); } if (!XDRChars(xdr, chars, nchars)) goto bad; if (xdr->mode == JSXDR_DECODE) { chars[nchars] = 0; *strp = JS_NewUCString(xdr->cx, chars, nchars); if (!*strp) goto bad; } return JS_TRUE; bad: if (xdr->mode == JSXDR_DECODE) JS_free(xdr->cx, chars); return JS_FALSE; }
js_InitAtomMap(JSContext *cx, JSAtomMap *map, JSAtomList *al) { JSAtom *atom, *next, **vector; uint32 count; atom = al->list; if (!atom) { map->vector = NULL; map->length = 0; return JS_TRUE; } count = al->count; if (count >= ATOM_INDEX_LIMIT) { JS_ReportError(cx, "too many literals"); return JS_FALSE; } vector = JS_malloc(cx, (size_t) count * sizeof *vector); if (!vector) return JS_FALSE; do { vector[atom->index] = atom; atom->flags &= ~ATOM_INDEXED; next = ATOM_NEXT(atom); ATOM_SET_NEXT(atom, NULL); } while ((atom = next) != NULL); al->list = NULL; al->count = 0; map->vector = vector; map->length = (jsatomid)count; return JS_TRUE; }
static size_t recv_body(void *ptr, size_t size, size_t nmem, void *data) { CurlState* state = (CurlState*) data; size_t length = size * nmem; char* tmp = NULL; if(!state->recvbuf) { state->recvlen = 4096; state->read = 0; state->recvbuf = JS_malloc(state->cx, state->recvlen); } if(!state->recvbuf) { return CURLE_WRITE_ERROR; } // +1 so we can add '\0' back up in the go function. while(length+1 > state->recvlen - state->read) state->recvlen *= 2; tmp = JS_realloc(state->cx, state->recvbuf, state->recvlen); if(!tmp) return CURLE_WRITE_ERROR; state->recvbuf = tmp; memcpy(state->recvbuf + state->read, ptr, length); state->read += length; return length; }
JSString* __Window_readLine (JSContext* cx, WINDOW* win, JSBool moveFirst, jsval x, jsval y) { char* string = (char*) JS_malloc(cx, 16*sizeof(char)); size_t length = 0; JS_BeginRequest(cx); JS_EnterLocalRootScope(cx); string[0] = (moveFirst ? mvwgetch(win, JSVAL_TO_INT(y), JSVAL_TO_INT(x)) : wgetch(win)); while (string[(++length)-1] != '\n') { if ((length+1) % 16) { string = (char*) JS_realloc(cx, string, (length+16+1)*sizeof(char)); } string[length] = (char) wgetch(win); } string[length-1] = '\0'; string = (char*) JS_realloc(cx, string, length*sizeof(char)); JSString* jsString = JS_NewString(cx, string, strlen(string)); JS_LeaveLocalRootScope(cx); JS_EndRequest(cx); return jsString; }
JSBool S_CCSequence::jsactions(JSContext *cx, uint32_t argc, jsval *vp) { // just like CCSequence::actions if (argc > 0) { jsval* argv = JS_ARGV(cx, vp); // get first element S_CCSequence* prev; JSGET_PTRSHELL(S_CCSequence, prev, JSVAL_TO_OBJECT(argv[0])); for (int i=1; i < argc; i++) { CCFiniteTimeAction *next; JSGET_PTRSHELL(CCFiniteTimeAction, next, JSVAL_TO_OBJECT(argv[i])); prev = (S_CCSequence *)CCSequence::actionOneTwo(prev, next); } // wrap prev in an action // temporary because it's just a wrapper for an autoreleased object // or worst case, it's an already binded object (if it's just one item in the array) pointerShell_t* pt = (pointerShell_t *)JS_malloc(cx, sizeof(pointerShell_t)); pt->flags = kPointerTemporary; pt->data = prev; JSObject* out = JS_NewObject(cx, S_CCSequence::jsClass, S_CCSequence::jsObject, NULL); prev->jsObject = out; JS_SetPrivate(out, pt); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(out)); return JS_TRUE; } JS_ReportError(cx, "must call with at least one element"); return JS_FALSE; }
char* enc_string(JSContext* cx, jsval arg, size_t* buflen) { JSString* str = NULL; jschar* src = NULL; char* bytes = NULL; size_t srclen = 0; size_t byteslen = 0; str = JS_ValueToString(cx, arg); if(!str) goto error; src = JS_GetStringChars(str); srclen = JS_GetStringLength(str); if(!enc_charbuf(src, srclen, NULL, &byteslen)) goto error; bytes = JS_malloc(cx, (byteslen) + 1); bytes[byteslen] = 0; if(!enc_charbuf(src, srclen, bytes, &byteslen)) goto error; if(buflen) *buflen = byteslen; goto success; error: if(bytes != NULL) JS_free(cx, bytes); bytes = NULL; success: return bytes; }
int js_PushLocalRoot(JSContext *cx, JSLocalRootStack *lrs, jsval v) { unsigned n, m; JSLocalRootChunk *lrc; n = lrs->rootCount; m = n & JSLRS_CHUNK_MASK; if (n == 0 || m != 0) { /* * At start of first chunk, or not at start of a non-first top chunk. * Check for lrs->rootCount overflow. */ if ((uint16)(n + 1) == 0) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_TOO_MANY_LOCAL_ROOTS); return -1; } lrc = lrs->topChunk; JS_ASSERT(n != 0 || lrc == &lrs->firstChunk); } else { /* * After lrs->firstChunk, trying to index at a power-of-two chunk * boundary: need a new chunk. */ lrc = (JSLocalRootChunk *) JS_malloc(cx, sizeof *lrc); if (!lrc) return -1; lrc->down = lrs->topChunk; lrs->topChunk = lrc; } lrs->rootCount = n + 1; lrc->roots[m] = v; return (int) m; }
static JSObject * define_JavaPackage(JSContext *cx, JSObject *parent_obj, const char *obj_name, const char *path, int flags, int access) { JSObject *package_obj; JavaPackage_Private *package; 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)); 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; }
JSBool js_EnterLocalRootScope(JSContext *cx) { JSLocalRootStack *lrs; int mark; lrs = cx->localRootStack; if (!lrs) { lrs = (JSLocalRootStack *) JS_malloc(cx, sizeof *lrs); if (!lrs) return JS_FALSE; lrs->scopeMark = JSLRS_NULL_MARK; lrs->rootCount = 0; lrs->topChunk = &lrs->firstChunk; lrs->firstChunk.down = NULL; cx->localRootStack = lrs; } /* Push lrs->scopeMark to save it for restore when leaving. */ mark = js_PushLocalRoot(cx, lrs, INT_TO_JSVAL(lrs->scopeMark)); if (mark < 0) return JS_FALSE; lrs->scopeMark = (uint16) mark; return JS_TRUE; }
static JSBool ejszlib_compress (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { EJS_CHECK_NUM_ARGS(cx,obj,1,argc); char* ctype; size_t len; STRING_TO_CHARVEC(argv[0],ctype,len); // todo: perhaps simply return an empty string if (!len) EJS_THROW_ERROR(cx,obj,"nothing to compress"); uLong destLen=compressBound(len); Byte* dest=(Byte *)JS_malloc(cx, destLen); if (!dest) return JS_FALSE; if (compress(dest, &destLen, (Byte*)ctype, len)!=Z_OK) { JS_free(cx,dest); EJS_THROW_ERROR(cx,obj,"compression failed"); } assert(destLen>0); dest=(Byte *)JS_realloc(cx,dest,destLen); RETSTR(dest,destLen,rval); }
JavaMemberDescriptor * jsj_GetJavaMemberDescriptor(JSContext *cx, JNIEnv *jEnv, JavaClassDescriptor *class_descriptor, jstring member_name_jstr) { JavaMemberDescriptor *member_descriptor; jsid id; if (!JavaStringToId(cx, jEnv, member_name_jstr, &id)) return NULL; member_descriptor = jsj_LookupJavaMemberDescriptorById(cx, jEnv, class_descriptor, id); if (member_descriptor) return member_descriptor; member_descriptor = JS_malloc(cx, sizeof(JavaMemberDescriptor)); if (!member_descriptor) return NULL; memset(member_descriptor, 0, sizeof(JavaMemberDescriptor)); member_descriptor->name = jsj_DupJavaStringUTF(cx, jEnv, member_name_jstr); if (!member_descriptor->name) { JS_free(cx, member_descriptor); return NULL; } member_descriptor->id = id; member_descriptor->next = class_descriptor->instance_members; class_descriptor->instance_members = member_descriptor; return member_descriptor; }
/** * Get the window object for a plugin instance. * * @param jEnv - JNIEnv on which the call is being made. * @param pJavaObject - Either a jobject or a pointer to a plugin instance * representing the java object. * @param pjobj - return value. This is a native js object * representing the window object of a frame * in which a applet/bean resides. */ NS_METHOD nsCLiveconnect::GetWindow(JNIEnv *jEnv, void *pJavaObject, void* principalsArray[], int numPrincipals, nsISupports *securitySupports, lcjsobject *pobj) { if(jEnv == NULL || JSJ_callbacks == NULL) { return NS_ERROR_FAILURE; } // associate this Java client with this LiveConnect connection. mJavaClient = pJavaObject; char *err_msg = NULL; JSContext *cx = NULL; JSObject *js_obj = NULL; JSErrorReporter saved_state = NULL; JSJavaThreadState *jsj_env = NULL; JSObjectHandle *handle = NULL; jsj_env = jsj_enter_js(jEnv, mJavaClient, NULL, &cx, NULL, &saved_state, principalsArray, numPrincipals, securitySupports); if (!jsj_env) return NS_ERROR_FAILURE; err_msg = NULL; AutoPushJSContext autopush(securitySupports, cx); if (NS_FAILED(autopush.ResultOfPush())) goto done; js_obj = JSJ_callbacks->map_java_object_to_js_object(jEnv, mJavaClient, &err_msg); if (!js_obj) { if (err_msg) { JS_ReportError(cx, err_msg); free(err_msg); } goto done; } #ifdef PRESERVE_JSOBJECT_IDENTITY *pobj = (jint)js_obj; #else /* FIXME: to handle PRESERVE_JSOBJECT_IDENTITY case, this needs to just return a raw JSObject reference. FIXME !!! */ /* Create a tiny stub object to act as the GC root that points to the JSObject from its netscape.javascript.JSObject counterpart. */ handle = (JSObjectHandle*)JS_malloc(cx, sizeof(JSObjectHandle)); if (handle != NULL) { handle->js_obj = js_obj; handle->rt = JS_GetRuntime(cx); } *pobj = (lcjsobject)handle; /* FIXME: what if the window is explicitly disposed of, how do we notify Java? */ #endif done: if (!jsj_exit_js(cx, jsj_env, saved_state)) return NS_ERROR_FAILURE; return NS_OK; }
component_mozilla_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { char *name, *type_str, *set_str, *prop_val; jsval type, func; if (!JSVAL_IS_STRING(id)) return JS_TRUE; name = JS_GetStringBytes(JSVAL_TO_STRING(id)); type_str = JS_malloc(cx, XP_STRLEN(lm_typePrefix_str) + XP_STRLEN(name) + 1); set_str = JS_malloc(cx, XP_STRLEN(lm_setPrefix_str) + XP_STRLEN(name) + 1); if (!type_str || !set_str) return JS_TRUE; XP_STRCPY(type_str, lm_typePrefix_str); XP_STRCAT(type_str, name); XP_STRCPY(set_str, lm_setPrefix_str); XP_STRCAT(set_str, name); if (!JS_GetProperty(cx, obj, type_str, &type) || !JSVAL_IS_INT(type) || !JS_GetProperty(cx, obj, set_str, &func)) return JS_TRUE; JS_free(cx, type_str); JS_free(cx, set_str); switch(JSVAL_TO_INT(type)) { case ARGTYPE_INT32: if (JSVAL_IS_INT(*vp)) ET_moz_CompSetterFunction((ETCompPropSetterFunc)JSVAL_TO_INT(func), name, (void*)JSVAL_TO_INT(*vp)); break; case ARGTYPE_BOOL: if (JSVAL_IS_BOOLEAN(*vp)) ET_moz_CompSetterFunction((ETCompPropSetterFunc)JSVAL_TO_INT(func), name, (void*)JSVAL_TO_BOOLEAN(*vp)); break; case ARGTYPE_STRING: if (JSVAL_IS_STRING(*vp)) { prop_val = JS_GetStringBytes(JSVAL_TO_STRING(*vp)); ET_moz_CompSetterFunction((ETCompPropSetterFunc)JSVAL_TO_INT(func), name, (void*)prop_val); } break; } return JS_TRUE; }
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; }
/* * The arguments from ap need to be packaged up into an array and stored * into the report struct. * * The format string addressed by the error number may contain operands * identified by the format {N}, where N is a decimal digit. Each of these * is to be replaced by the Nth argument from the va_list. The complete * message is placed into reportp->ucmessage converted to a JSString. * * Returns true if the expansion succeeds (can fail if out of memory). */ JSBool js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback, void *userRef, const uintN errorNumber, char **messagep, JSErrorReport *reportp, JSBool *warningp, JSBool charArgs, va_list ap) { const JSErrorFormatString *efs; int i; int argCount; *warningp = JSREPORT_IS_WARNING(reportp->flags); if (*warningp && JS_HAS_WERROR_OPTION(cx)) { reportp->flags &= ~JSREPORT_WARNING; *warningp = JS_FALSE; } *messagep = NULL; /* Most calls supply js_GetErrorMessage; if this is so, assume NULL. */ if (!callback || callback == js_GetErrorMessage) efs = js_GetLocalizedErrorMessage(cx, userRef, NULL, errorNumber); else efs = callback(userRef, NULL, errorNumber); if (efs) { size_t totalArgsLength = 0; size_t argLengths[10]; /* only {0} thru {9} supported */ argCount = efs->argCount; JS_ASSERT(argCount <= 10); if (argCount > 0) { /* * Gather the arguments into an array, and accumulate * their sizes. We allocate 1 more than necessary and * null it out to act as the caboose when we free the * pointers later. */ reportp->messageArgs = (const jschar **) JS_malloc(cx, sizeof(jschar *) * (argCount + 1)); if (!reportp->messageArgs) return JS_FALSE; reportp->messageArgs[argCount] = NULL; for (i = 0; i < argCount; i++) { if (charArgs) { char *charArg = va_arg(ap, char *); size_t charArgLength = strlen(charArg); reportp->messageArgs[i] = js_InflateString(cx, charArg, &charArgLength); if (!reportp->messageArgs[i]) goto error; } else { reportp->messageArgs[i] = va_arg(ap, jschar *); } argLengths[i] = js_strlen(reportp->messageArgs[i]); totalArgsLength += argLengths[i]; } /* NULL-terminate for easy copying. */ reportp->messageArgs[i] = NULL; }
JS_GetPropertyDescArray(JSContext *cx, JSObject *obj, JSPropertyDescArray *pda) { JSClass *clasp; JSScope *scope; uint32 i, n; JSPropertyDesc *pd; JSScopeProperty *sprop; clasp = OBJ_GET_CLASS(cx, obj); if (!OBJ_IS_NATIVE(obj) || (clasp->flags & JSCLASS_NEW_ENUMERATE)) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_DESCRIBE_PROPS, clasp->name); return JS_FALSE; } if (!clasp->enumerate(cx, obj)) return JS_FALSE; /* have no props, or object's scope has not mutated from that of proto */ scope = OBJ_SCOPE(obj); if (scope->object != obj || scope->entryCount == 0) { pda->length = 0; pda->array = NULL; return JS_TRUE; } n = scope->entryCount; if (n > scope->map.nslots) n = scope->map.nslots; pd = (JSPropertyDesc *) JS_malloc(cx, (size_t)n * sizeof(JSPropertyDesc)); if (!pd) return JS_FALSE; i = 0; for (sprop = SCOPE_LAST_PROP(scope); sprop; sprop = sprop->parent) { if (SCOPE_HAD_MIDDLE_DELETE(scope) && !SCOPE_HAS_PROPERTY(scope, sprop)) continue; if (!js_AddRoot(cx, &pd[i].id, NULL)) goto bad; if (!js_AddRoot(cx, &pd[i].value, NULL)) goto bad; if (!JS_GetPropertyDesc(cx, obj, sprop, &pd[i])) goto bad; if ((pd[i].flags & JSPD_ALIAS) && !js_AddRoot(cx, &pd[i].alias, NULL)) goto bad; if (++i == n) break; } pda->length = i; pda->array = pd; return JS_TRUE; bad: pda->length = i + 1; pda->array = pd; JS_PutPropertyDescArray(cx, pda); return JS_FALSE; }
// convert a readable to a JSString, copying string data // static jsval XPCStringConvert::ReadableToJSVal(JSContext *cx, const nsAString &readable, nsStringBuffer** sharedBuffer) { JSString *str; *sharedBuffer = nullptr; uint32_t length = readable.Length(); if (length == 0) return JS_GetEmptyStringValue(cx); nsStringBuffer *buf = nsStringBuffer::FromString(readable); if (buf) { if (buf == sCachedBuffer && js::GetGCThingCompartment(sCachedString) == js::GetContextCompartment(cx)) { // We're done. Just return our existing string. return JS::StringValue(sCachedString); } // yay, we can share the string's buffer! str = JS_NewExternalString(cx, reinterpret_cast<jschar *>(buf->Data()), length, &sDOMStringFinalizer); if (str) { *sharedBuffer = buf; sCachedString = str; sCachedBuffer = buf; } } else { // blech, have to copy. jschar *chars = reinterpret_cast<jschar *> (JS_malloc(cx, (length + 1) * sizeof(jschar))); if (!chars) return JSVAL_NULL; if (length && !CopyUnicodeTo(readable, 0, reinterpret_cast<PRUnichar *>(chars), length)) { JS_free(cx, chars); return JSVAL_NULL; } chars[length] = 0; str = JS_NewUCString(cx, chars, length); if (!str) JS_free(cx, chars); } return str ? STRING_TO_JSVAL(str) : JSVAL_NULL; }