/* |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 JSBool FindConstructor(JSContext *cx, JSClass *clasp, jsval *vp) { JSAtom *atom; JSObject *obj, *tmp; JSBool ok; PR_ASSERT(JS_IS_LOCKED(cx)); /* XXX pre-atomize in JS_InitClass! */ atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0); if (!atom) return JS_FALSE; if (cx->fp && (tmp = cx->fp->scopeChain)) { /* Find the topmost object in the scope chain. */ do { obj = tmp; tmp = OBJ_GET_PARENT(obj); } while (tmp); } else { obj = cx->globalObject; if (!obj) { *vp = JSVAL_VOID; return JS_TRUE; } } ok = (js_GetProperty(cx, obj, (jsval)atom, vp) != NULL); js_DropAtom(cx, atom); return JS_TRUE; }
JSBool js_InitScanner(JSContext *cx) { struct keyword *kw; JSAtom *atom; for (kw = keywords; kw->name; kw++) { atom = js_Atomize(cx, kw->name, strlen(kw->name), ATOM_PINNED); if (!atom) return JS_FALSE; ATOM_SET_KEYWORD(atom, kw); } return JS_TRUE; }
JSBool js_InitCommonAtoms(JSContext *cx) { JSAtomState *state = &cx->runtime->atomState; uintN i; JSAtom **atoms; atoms = COMMON_ATOMS_START(state); for (i = 0; i < JS_ARRAY_LENGTH(js_common_atom_names); i++, atoms++) { *atoms = js_Atomize(cx, js_common_atom_names[i], strlen(js_common_atom_names[i]), ATOM_PINNED); if (!*atoms) return JS_FALSE; } JS_ASSERT((uint8 *)atoms - (uint8 *)state == LAZY_ATOM_OFFSET_START); memset(atoms, 0, ATOM_OFFSET_LIMIT - LAZY_ATOM_OFFSET_START); return JS_TRUE; }
/* * FIXME: This performs lossy conversion and we need to switch to * js_XDRStringAtom while allowing to read older XDR files. See bug 325202. */ JSBool js_XDRCStringAtom(JSXDRState *xdr, JSAtom **atomp) { char *bytes; uint32 nbytes; JSAtom *atom; JSContext *cx; void *mark; if (xdr->mode == JSXDR_ENCODE) { JS_ASSERT(ATOM_IS_STRING(*atomp)); bytes = JS_GetStringBytes(ATOM_TO_STRING(*atomp)); return JS_XDRCString(xdr, &bytes); } /* * Inline JS_XDRCString when decoding not to malloc temporary buffer * just to free it after atomization. See bug 321985. */ if (!JS_XDRUint32(xdr, &nbytes)) return JS_FALSE; atom = NULL; cx = xdr->cx; mark = JS_ARENA_MARK(&cx->tempPool); JS_ARENA_ALLOCATE_CAST(bytes, char *, &cx->tempPool, nbytes * sizeof *bytes); if (!bytes) JS_ReportOutOfMemory(cx); else if (JS_XDRBytes(xdr, bytes, nbytes)) atom = js_Atomize(cx, bytes, nbytes, 0); JS_ARENA_RELEASE(&cx->tempPool, mark); if (!atom) return JS_FALSE; *atomp = atom; return JS_TRUE; }
JSObject * js_InitExceptionClasses(JSContext *cx, JSObject *obj) { int i; JSObject *protos[JSEXN_LIMIT]; /* Initialize the prototypes first. */ for (i = 0; exceptions[i].name != 0; i++) { JSAtom *atom; JSFunction *fun; JSString *nameString; int protoIndex = exceptions[i].protoIndex; /* Make the prototype for the current constructor name. */ protos[i] = js_NewObject(cx, &ExceptionClass, (protoIndex != JSEXN_NONE) ? protos[protoIndex] : NULL, obj); if (!protos[i]) return NULL; /* So exn_finalize knows whether to destroy private data. */ OBJ_SET_SLOT(cx, protos[i], JSSLOT_PRIVATE, JSVAL_VOID); atom = js_Atomize(cx, exceptions[i].name, strlen(exceptions[i].name), 0); if (!atom) return NULL; /* Make a constructor function for the current name. */ fun = js_DefineFunction(cx, obj, atom, exceptions[i].native, 3, 0); if (!fun) return NULL; /* Make this constructor make objects of class Exception. */ fun->clasp = &ExceptionClass; /* Make the prototype and constructor links. */ if (!js_SetClassPrototype(cx, fun->object, protos[i], JSPROP_READONLY | JSPROP_PERMANENT)) { return NULL; } /* proto bootstrap bit from JS_InitClass omitted. */ nameString = JS_NewStringCopyZ(cx, exceptions[i].name); if (!nameString) return NULL; /* Add the name property to the prototype. */ if (!JS_DefineProperty(cx, protos[i], js_name_str, STRING_TO_JSVAL(nameString), NULL, NULL, JSPROP_ENUMERATE)) { return NULL; } } /* * Add an empty message property. (To Exception.prototype only, * because this property will be the same for all the exception * protos.) */ if (!JS_DefineProperty(cx, protos[0], js_message_str, STRING_TO_JSVAL(cx->runtime->emptyString), NULL, NULL, JSPROP_ENUMERATE)) { return NULL; } if (!JS_DefineProperty(cx, protos[0], js_filename_str, STRING_TO_JSVAL(cx->runtime->emptyString), NULL, NULL, JSPROP_ENUMERATE)) { return NULL; } if (!JS_DefineProperty(cx, protos[0], js_lineno_str, INT_TO_JSVAL(0), NULL, NULL, JSPROP_ENUMERATE)) { return NULL; } /* * Add methods only to Exception.prototype, because ostensibly all * exception types delegate to that. */ if (!JS_DefineFunctions(cx, protos[0], exception_methods)) return NULL; return protos[0]; }