| /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| * vim: set ts=8 sts=4 et sw=4 tw=99: |
| * This Source Code Form is subject to the terms of the Mozilla Public |
| * License, v. 2.0. If a copy of the MPL was not distributed with this |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| |
| /* |
| * JavaScript API. |
| */ |
| |
| #include "jsapi.h" |
| |
| #include "mozilla/FloatingPoint.h" |
| #include "mozilla/PodOperations.h" |
| #include "mozilla/UniquePtr.h" |
| |
| #include <ctype.h> |
| #include <stdarg.h> |
| #include <string.h> |
| #include <sys/stat.h> |
| |
| #include "jsarray.h" |
| #include "jsatom.h" |
| #include "jsbool.h" |
| #include "jscntxt.h" |
| #include "jsdate.h" |
| #include "jsexn.h" |
| #include "jsfriendapi.h" |
| #include "jsfun.h" |
| #include "jsgc.h" |
| #include "jsiter.h" |
| #include "jslock.h" |
| #include "jsmath.h" |
| #include "jsnum.h" |
| #include "jsobj.h" |
| #include "json.h" |
| #include "jsprf.h" |
| #include "jsscript.h" |
| #include "jsstr.h" |
| #include "jstypes.h" |
| #include "jsutil.h" |
| #include "jswatchpoint.h" |
| #include "jsweakmap.h" |
| #include "jswrapper.h" |
| |
| #include "asmjs/AsmJSLink.h" |
| #include "builtin/AtomicsObject.h" |
| #include "builtin/Eval.h" |
| #include "builtin/Intl.h" |
| #include "builtin/MapObject.h" |
| #include "builtin/RegExp.h" |
| #include "builtin/SymbolObject.h" |
| #ifdef ENABLE_BINARYDATA |
| #include "builtin/SIMD.h" |
| #include "builtin/TypedObject.h" |
| #endif |
| #include "frontend/BytecodeCompiler.h" |
| #include "frontend/FullParseHandler.h" // for JS_BufferIsCompileableUnit |
| #include "frontend/Parser.h" // for JS_BufferIsCompileableUnit |
| #include "gc/Marking.h" |
| #include "jit/JitCommon.h" |
| #include "js/CharacterEncoding.h" |
| #include "js/Conversions.h" |
| #include "js/Date.h" |
| #include "js/Initialization.h" |
| #include "js/Proxy.h" |
| #include "js/SliceBudget.h" |
| #include "js/StructuredClone.h" |
| #include "vm/DateObject.h" |
| #include "vm/Debugger.h" |
| #include "vm/ErrorObject.h" |
| #include "vm/HelperThreads.h" |
| #include "vm/Interpreter.h" |
| #include "vm/RegExpStatics.h" |
| #include "vm/Runtime.h" |
| #include "vm/SavedStacks.h" |
| #include "vm/ScopeObject.h" |
| #include "vm/Shape.h" |
| #include "vm/StopIterationObject.h" |
| #include "vm/StringBuffer.h" |
| #include "vm/Symbol.h" |
| #include "vm/TypedArrayCommon.h" |
| #include "vm/WrapperObject.h" |
| #include "vm/Xdr.h" |
| |
| #include "jsatominlines.h" |
| #include "jsfuninlines.h" |
| #include "jsscriptinlines.h" |
| |
| #include "vm/Interpreter-inl.h" |
| #include "vm/NativeObject-inl.h" |
| #include "vm/SavedStacks-inl.h" |
| #include "vm/String-inl.h" |
| |
| using namespace js; |
| using namespace js::gc; |
| |
| using mozilla::Maybe; |
| using mozilla::PodCopy; |
| using mozilla::PodZero; |
| using mozilla::UniquePtr; |
| |
| using JS::AutoGCRooter; |
| using JS::ToInt32; |
| using JS::ToInteger; |
| using JS::ToUint32; |
| |
| using js::frontend::Parser; |
| |
| #ifdef HAVE_VA_LIST_AS_ARRAY |
| #define JS_ADDRESSOF_VA_LIST(ap) ((va_list*)(ap)) |
| #else |
| #define JS_ADDRESSOF_VA_LIST(ap) (&(ap)) |
| #endif |
| |
| bool |
| JS::CallArgs::requireAtLeast(JSContext* cx, const char* fnname, unsigned required) const |
| { |
| if (length() < required) { |
| char numArgsStr[40]; |
| JS_snprintf(numArgsStr, sizeof numArgsStr, "%u", required - 1); |
| JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_MORE_ARGS_NEEDED, |
| fnname, numArgsStr, required == 2 ? "" : "s"); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| static bool |
| ErrorTakesArguments(unsigned msg) |
| { |
| MOZ_ASSERT(msg < JSErr_Limit); |
| unsigned argCount = js_ErrorFormatString[msg].argCount; |
| MOZ_ASSERT(argCount <= 2); |
| return argCount == 1 || argCount == 2; |
| } |
| |
| static bool |
| ErrorTakesObjectArgument(unsigned msg) |
| { |
| MOZ_ASSERT(msg < JSErr_Limit); |
| unsigned argCount = js_ErrorFormatString[msg].argCount; |
| MOZ_ASSERT(argCount <= 2); |
| return argCount == 2; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::reportStrictErrorOrWarning(JSContext* cx, HandleObject obj, HandleId id, |
| bool strict) |
| { |
| static_assert(unsigned(OkCode) == unsigned(JSMSG_NOT_AN_ERROR), |
| "unsigned value of OkCode must not be an error code"); |
| MOZ_ASSERT(code_ != Uninitialized); |
| MOZ_ASSERT(!ok()); |
| |
| unsigned flags = strict ? JSREPORT_ERROR : (JSREPORT_WARNING | JSREPORT_STRICT); |
| if (code_ == JSMSG_OBJECT_NOT_EXTENSIBLE || code_ == JSMSG_SET_NON_OBJECT_RECEIVER) { |
| RootedValue val(cx, ObjectValue(*obj)); |
| return ReportValueErrorFlags(cx, flags, code_, JSDVG_IGNORE_STACK, val, |
| nullptr, nullptr, nullptr); |
| } |
| if (ErrorTakesArguments(code_)) { |
| RootedValue idv(cx, IdToValue(id)); |
| RootedString str(cx, ValueToSource(cx, idv)); |
| if (!str) |
| return false; |
| |
| JSAutoByteString propName(cx, str); |
| if (!propName) |
| return false; |
| |
| if (ErrorTakesObjectArgument(code_)) { |
| return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_, |
| obj->getClass()->name, propName.ptr()); |
| } |
| |
| return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_, |
| propName.ptr()); |
| } |
| return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::reportStrictErrorOrWarning(JSContext* cx, HandleObject obj, bool strict) |
| { |
| MOZ_ASSERT(code_ != Uninitialized); |
| MOZ_ASSERT(!ok()); |
| MOZ_ASSERT(!ErrorTakesArguments(code_)); |
| |
| unsigned flags = strict ? JSREPORT_ERROR : (JSREPORT_WARNING | JSREPORT_STRICT); |
| return JS_ReportErrorFlagsAndNumber(cx, flags, GetErrorMessage, nullptr, code_); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantRedefineProp() |
| { |
| return fail(JSMSG_CANT_REDEFINE_PROP); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failReadOnly() |
| { |
| return fail(JSMSG_READ_ONLY); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failGetterOnly() |
| { |
| return fail(JSMSG_GETTER_ONLY); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantDelete() |
| { |
| return fail(JSMSG_CANT_DELETE); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantSetInterposed() |
| { |
| return fail(JSMSG_CANT_SET_INTERPOSED); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantDefineWindowElement() |
| { |
| return fail(JSMSG_CANT_DEFINE_WINDOW_ELEMENT); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantDeleteWindowElement() |
| { |
| return fail(JSMSG_CANT_DELETE_WINDOW_ELEMENT); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantDeleteWindowNamedProperty() |
| { |
| return fail(JSMSG_CANT_DELETE_WINDOW_NAMED_PROPERTY); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantPreventExtensions() |
| { |
| return fail(JSMSG_CANT_PREVENT_EXTENSIONS); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failCantSetProto() |
| { |
| return fail(JSMSG_CANT_SET_PROTO); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failNoNamedSetter() |
| { |
| return fail(JSMSG_NO_NAMED_SETTER); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ObjectOpResult::failNoIndexedSetter() |
| { |
| return fail(JSMSG_NO_INDEXED_SETTER); |
| } |
| |
| JS_PUBLIC_API(int64_t) |
| JS_Now() |
| { |
| return PRMJ_Now(); |
| } |
| |
| JS_PUBLIC_API(Value) |
| JS_GetNaNValue(JSContext* cx) |
| { |
| return cx->runtime()->NaNValue; |
| } |
| |
| JS_PUBLIC_API(Value) |
| JS_GetNegativeInfinityValue(JSContext* cx) |
| { |
| return cx->runtime()->negativeInfinityValue; |
| } |
| |
| JS_PUBLIC_API(Value) |
| JS_GetPositiveInfinityValue(JSContext* cx) |
| { |
| return cx->runtime()->positiveInfinityValue; |
| } |
| |
| JS_PUBLIC_API(Value) |
| JS_GetEmptyStringValue(JSContext* cx) |
| { |
| return StringValue(cx->runtime()->emptyString); |
| } |
| |
| JS_PUBLIC_API(JSString*) |
| JS_GetEmptyString(JSRuntime* rt) |
| { |
| MOZ_ASSERT(rt->hasContexts()); |
| return rt->emptyString; |
| } |
| |
| namespace js { |
| |
| void |
| AssertHeapIsIdle(JSRuntime* rt) |
| { |
| MOZ_ASSERT(!rt->isHeapBusy()); |
| } |
| |
| void |
| AssertHeapIsIdle(JSContext* cx) |
| { |
| AssertHeapIsIdle(cx->runtime()); |
| } |
| |
| } // namespace js |
| |
| static void |
| AssertHeapIsIdleOrIterating(JSRuntime* rt) |
| { |
| MOZ_ASSERT(!rt->isHeapCollecting()); |
| } |
| |
| static void |
| AssertHeapIsIdleOrIterating(JSContext* cx) |
| { |
| AssertHeapIsIdleOrIterating(cx->runtime()); |
| } |
| |
| static void |
| AssertHeapIsIdleOrStringIsFlat(JSContext* cx, JSString* str) |
| { |
| /* |
| * We allow some functions to be called during a GC as long as the argument |
| * is a flat string, since that will not cause allocation. |
| */ |
| MOZ_ASSERT_IF(cx->runtime()->isHeapBusy(), str->isFlat()); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_ValueToObject(JSContext* cx, HandleValue value, MutableHandleObject objp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value); |
| if (value.isNullOrUndefined()) { |
| objp.set(nullptr); |
| return true; |
| } |
| JSObject* obj = ToObject(cx, value); |
| if (!obj) |
| return false; |
| objp.set(obj); |
| return true; |
| } |
| |
| JS_PUBLIC_API(JSFunction*) |
| JS_ValueToFunction(JSContext* cx, HandleValue value) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value); |
| return ReportIfNotFunction(cx, value); |
| } |
| |
| JS_PUBLIC_API(JSFunction*) |
| JS_ValueToConstructor(JSContext* cx, HandleValue value) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value); |
| return ReportIfNotFunction(cx, value); |
| } |
| |
| JS_PUBLIC_API(JSString*) |
| JS_ValueToSource(JSContext* cx, HandleValue value) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value); |
| return ValueToSource(cx, value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DoubleIsInt32(double d, int32_t* ip) |
| { |
| return mozilla::NumberIsInt32(d, ip); |
| } |
| |
| JS_PUBLIC_API(JSType) |
| JS_TypeOfValue(JSContext* cx, HandleValue value) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value); |
| return TypeOfValue(value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_StrictlyEqual(JSContext* cx, HandleValue value1, HandleValue value2, bool* equal) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value1, value2); |
| MOZ_ASSERT(equal); |
| return StrictlyEqual(cx, value1, value2, equal); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_LooselyEqual(JSContext* cx, HandleValue value1, HandleValue value2, bool* equal) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value1, value2); |
| MOZ_ASSERT(equal); |
| return LooselyEqual(cx, value1, value2, equal); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SameValue(JSContext* cx, HandleValue value1, HandleValue value2, bool* same) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value1, value2); |
| MOZ_ASSERT(same); |
| return SameValue(cx, value1, value2, same); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_IsBuiltinEvalFunction(JSFunction* fun) |
| { |
| return IsAnyBuiltinEval(fun); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_IsBuiltinFunctionConstructor(JSFunction* fun) |
| { |
| return fun->isBuiltinFunctionConstructor(); |
| } |
| |
| /************************************************************************/ |
| |
| #ifdef DEBUG |
| JS_FRIEND_API(bool) |
| JS::isGCEnabled() |
| { |
| return !TlsPerThreadData.get()->suppressGC; |
| } |
| #else |
| JS_FRIEND_API(bool) JS::isGCEnabled() { return true; } |
| #endif |
| |
| JS_PUBLIC_API(JSRuntime*) |
| JS_NewRuntime(uint32_t maxbytes, uint32_t maxNurseryBytes, JSRuntime* parentRuntime) |
| { |
| MOZ_ASSERT(JS::detail::libraryInitState == JS::detail::InitState::Running, |
| "must call JS_Init prior to creating any JSRuntimes"); |
| |
| // Make sure that all parent runtimes are the topmost parent. |
| while (parentRuntime && parentRuntime->parentRuntime) |
| parentRuntime = parentRuntime->parentRuntime; |
| |
| JSRuntime* rt = js_new<JSRuntime>(parentRuntime); |
| if (!rt) |
| return nullptr; |
| |
| if (!rt->init(maxbytes, maxNurseryBytes)) { |
| JS_DestroyRuntime(rt); |
| return nullptr; |
| } |
| |
| return rt; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_DestroyRuntime(JSRuntime* rt) |
| { |
| js_delete(rt); |
| } |
| |
| static JS_CurrentEmbedderTimeFunction currentEmbedderTimeFunction; |
| |
| JS_PUBLIC_API(void) |
| JS_SetCurrentEmbedderTimeFunction(JS_CurrentEmbedderTimeFunction timeFn) |
| { |
| currentEmbedderTimeFunction = timeFn; |
| } |
| |
| JS_PUBLIC_API(double) |
| JS_GetCurrentEmbedderTime() |
| { |
| if (currentEmbedderTimeFunction) |
| return currentEmbedderTimeFunction(); |
| return PRMJ_Now() / static_cast<double>(PRMJ_USEC_PER_MSEC); |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetRuntimePrivate(JSRuntime* rt) |
| { |
| return rt->data; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetRuntimePrivate(JSRuntime* rt, void* data) |
| { |
| rt->data = data; |
| } |
| |
| static void |
| StartRequest(JSContext* cx) |
| { |
| JSRuntime* rt = cx->runtime(); |
| MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt)); |
| |
| if (rt->requestDepth) { |
| rt->requestDepth++; |
| } else { |
| /* Indicate that a request is running. */ |
| rt->requestDepth = 1; |
| rt->triggerActivityCallback(true); |
| } |
| } |
| |
| static void |
| StopRequest(JSContext* cx) |
| { |
| JSRuntime* rt = cx->runtime(); |
| MOZ_ASSERT(CurrentThreadCanAccessRuntime(rt)); |
| |
| MOZ_ASSERT(rt->requestDepth != 0); |
| if (rt->requestDepth != 1) { |
| rt->requestDepth--; |
| } else { |
| rt->requestDepth = 0; |
| rt->triggerActivityCallback(false); |
| } |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_BeginRequest(JSContext* cx) |
| { |
| cx->outstandingRequests++; |
| StartRequest(cx); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_EndRequest(JSContext* cx) |
| { |
| MOZ_ASSERT(cx->outstandingRequests != 0); |
| cx->outstandingRequests--; |
| StopRequest(cx); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetContextCallback(JSRuntime* rt, JSContextCallback cxCallback, void* data) |
| { |
| rt->cxCallback = cxCallback; |
| rt->cxCallbackData = data; |
| } |
| |
| JS_PUBLIC_API(JSContext*) |
| JS_NewContext(JSRuntime* rt, size_t stackChunkSize) |
| { |
| return NewContext(rt, stackChunkSize); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_DestroyContext(JSContext* cx) |
| { |
| MOZ_ASSERT(!cx->compartment()); |
| DestroyContext(cx, DCM_FORCE_GC); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_DestroyContextNoGC(JSContext* cx) |
| { |
| MOZ_ASSERT(!cx->compartment()); |
| DestroyContext(cx, DCM_NO_GC); |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetContextPrivate(JSContext* cx) |
| { |
| return cx->data; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetContextPrivate(JSContext* cx, void* data) |
| { |
| cx->data = data; |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetSecondContextPrivate(JSContext* cx) |
| { |
| return cx->data2; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetSecondContextPrivate(JSContext* cx, void* data) |
| { |
| cx->data2 = data; |
| } |
| |
| JS_PUBLIC_API(JSRuntime*) |
| JS_GetRuntime(JSContext* cx) |
| { |
| return cx->runtime(); |
| } |
| |
| JS_PUBLIC_API(JSRuntime*) |
| JS_GetParentRuntime(JSContext* cx) |
| { |
| JSRuntime* rt = cx->runtime(); |
| return rt->parentRuntime ? rt->parentRuntime : rt; |
| } |
| |
| JS_PUBLIC_API(JSContext*) |
| JS_ContextIterator(JSRuntime* rt, JSContext** iterp) |
| { |
| JSContext* cx = *iterp; |
| cx = cx ? cx->getNext() : rt->contextList.getFirst(); |
| *iterp = cx; |
| return cx; |
| } |
| |
| JS_PUBLIC_API(JSVersion) |
| JS_GetVersion(JSContext* cx) |
| { |
| return VersionNumber(cx->findVersion()); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetVersionForCompartment(JSCompartment* compartment, JSVersion version) |
| { |
| compartment->options().setVersion(version); |
| } |
| |
| static const struct v2smap { |
| JSVersion version; |
| const char* string; |
| } v2smap[] = { |
| {JSVERSION_ECMA_3, "ECMAv3"}, |
| {JSVERSION_1_6, "1.6"}, |
| {JSVERSION_1_7, "1.7"}, |
| {JSVERSION_1_8, "1.8"}, |
| {JSVERSION_ECMA_5, "ECMAv5"}, |
| {JSVERSION_DEFAULT, js_default_str}, |
| {JSVERSION_DEFAULT, "1.0"}, |
| {JSVERSION_DEFAULT, "1.1"}, |
| {JSVERSION_DEFAULT, "1.2"}, |
| {JSVERSION_DEFAULT, "1.3"}, |
| {JSVERSION_DEFAULT, "1.4"}, |
| {JSVERSION_DEFAULT, "1.5"}, |
| {JSVERSION_UNKNOWN, nullptr}, /* must be last, nullptr is sentinel */ |
| }; |
| |
| JS_PUBLIC_API(const char*) |
| JS_VersionToString(JSVersion version) |
| { |
| int i; |
| |
| for (i = 0; v2smap[i].string; i++) |
| if (v2smap[i].version == version) |
| return v2smap[i].string; |
| return "unknown"; |
| } |
| |
| JS_PUBLIC_API(JSVersion) |
| JS_StringToVersion(const char* string) |
| { |
| int i; |
| |
| for (i = 0; v2smap[i].string; i++) |
| if (strcmp(v2smap[i].string, string) == 0) |
| return v2smap[i].version; |
| return JSVERSION_UNKNOWN; |
| } |
| |
| JS_PUBLIC_API(JS::RuntimeOptions&) |
| JS::RuntimeOptionsRef(JSRuntime* rt) |
| { |
| return rt->options(); |
| } |
| |
| JS_PUBLIC_API(JS::RuntimeOptions&) |
| JS::RuntimeOptionsRef(JSContext* cx) |
| { |
| return cx->runtime()->options(); |
| } |
| |
| JS_PUBLIC_API(JS::ContextOptions&) |
| JS::ContextOptionsRef(JSContext* cx) |
| { |
| return cx->options(); |
| } |
| |
| JS_PUBLIC_API(const char*) |
| JS_GetImplementationVersion(void) |
| { |
| return "JavaScript-C" MOZILLA_VERSION; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetDestroyCompartmentCallback(JSRuntime* rt, JSDestroyCompartmentCallback callback) |
| { |
| rt->destroyCompartmentCallback = callback; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetDestroyZoneCallback(JSRuntime* rt, JSZoneCallback callback) |
| { |
| rt->destroyZoneCallback = callback; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetSweepZoneCallback(JSRuntime* rt, JSZoneCallback callback) |
| { |
| rt->sweepZoneCallback = callback; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetCompartmentNameCallback(JSRuntime* rt, JSCompartmentNameCallback callback) |
| { |
| rt->compartmentNameCallback = callback; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetWrapObjectCallbacks(JSRuntime* rt, const JSWrapObjectCallbacks* callbacks) |
| { |
| rt->wrapObjectCallbacks = callbacks; |
| } |
| |
| JS_PUBLIC_API(JSCompartment*) |
| JS_EnterCompartment(JSContext* cx, JSObject* target) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| JSCompartment* oldCompartment = cx->compartment(); |
| cx->enterCompartment(target->compartment()); |
| return oldCompartment; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_LeaveCompartment(JSContext* cx, JSCompartment* oldCompartment) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| cx->leaveCompartment(oldCompartment); |
| } |
| |
| JSAutoCompartment::JSAutoCompartment(JSContext* cx, JSObject* target |
| MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) |
| : cx_(cx), |
| oldCompartment_(cx->compartment()) |
| { |
| AssertHeapIsIdleOrIterating(cx_); |
| MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
| cx_->enterCompartment(target->compartment()); |
| } |
| |
| JSAutoCompartment::JSAutoCompartment(JSContext* cx, JSScript* target |
| MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) |
| : cx_(cx), |
| oldCompartment_(cx->compartment()) |
| { |
| AssertHeapIsIdleOrIterating(cx_); |
| MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
| cx_->enterCompartment(target->compartment()); |
| } |
| |
| |
| JSAutoCompartment::~JSAutoCompartment() |
| { |
| cx_->leaveCompartment(oldCompartment_); |
| } |
| |
| JSAutoNullableCompartment::JSAutoNullableCompartment(JSContext* cx, |
| JSObject* targetOrNull |
| MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL) |
| : cx_(cx), |
| oldCompartment_(cx->compartment()) |
| { |
| AssertHeapIsIdleOrIterating(cx_); |
| MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
| if (targetOrNull) { |
| cx_->enterCompartment(targetOrNull->compartment()); |
| } else { |
| cx_->enterNullCompartment(); |
| } |
| } |
| |
| JSAutoNullableCompartment::~JSAutoNullableCompartment() |
| { |
| cx_->leaveCompartment(oldCompartment_); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetCompartmentPrivate(JSCompartment* compartment, void* data) |
| { |
| compartment->data = data; |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetCompartmentPrivate(JSCompartment* compartment) |
| { |
| return compartment->data; |
| } |
| |
| JS_PUBLIC_API(JSAddonId*) |
| JS::NewAddonId(JSContext* cx, HandleString str) |
| { |
| return static_cast<JSAddonId*>(JS_AtomizeAndPinJSString(cx, str)); |
| } |
| |
| JS_PUBLIC_API(JSString*) |
| JS::StringOfAddonId(JSAddonId* id) |
| { |
| return id; |
| } |
| |
| JS_PUBLIC_API(JSAddonId*) |
| JS::AddonIdOfObject(JSObject* obj) |
| { |
| return obj->compartment()->addonId; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetZoneUserData(JS::Zone* zone, void* data) |
| { |
| zone->data = data; |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetZoneUserData(JS::Zone* zone) |
| { |
| return zone->data; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_WrapObject(JSContext* cx, MutableHandleObject objp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| if (objp) |
| JS::ExposeObjectToActiveJS(objp); |
| return cx->compartment()->wrap(cx, objp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_WrapValue(JSContext* cx, MutableHandleValue vp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| JS::ExposeValueToActiveJS(vp); |
| return cx->compartment()->wrap(cx, vp); |
| } |
| |
| /* |
| * Identity remapping. Not for casual consumers. |
| * |
| * Normally, an object's contents and its identity are inextricably linked. |
| * Identity is determined by the address of the JSObject* in the heap, and |
| * the contents are what is located at that address. Transplanting allows these |
| * concepts to be separated through a combination of swapping (exchanging the |
| * contents of two same-compartment objects) and remapping cross-compartment |
| * identities by altering wrappers. |
| * |
| * The |origobj| argument should be the object whose identity needs to be |
| * remapped, usually to another compartment. The contents of |origobj| are |
| * destroyed. |
| * |
| * The |target| argument serves two purposes: |
| * |
| * First, |target| serves as a hint for the new identity of the object. The new |
| * identity object will always be in the same compartment as |target|, but |
| * if that compartment already had an object representing |origobj| (either a |
| * cross-compartment wrapper for it, or |origobj| itself if the two arguments |
| * are same-compartment), the existing object is used. Otherwise, |target| |
| * itself is used. To avoid ambiguity, JS_TransplantObject always returns the |
| * new identity. |
| * |
| * Second, the new identity object's contents will be those of |target|. A swap() |
| * is used to make this happen if an object other than |target| is used. |
| * |
| * We don't have a good way to recover from failure in this function, so |
| * we intentionally crash instead. |
| */ |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_TransplantObject(JSContext* cx, HandleObject origobj, HandleObject target) |
| { |
| AssertHeapIsIdle(cx); |
| MOZ_ASSERT(origobj != target); |
| MOZ_ASSERT(!origobj->is<CrossCompartmentWrapperObject>()); |
| MOZ_ASSERT(!target->is<CrossCompartmentWrapperObject>()); |
| |
| RootedValue origv(cx, ObjectValue(*origobj)); |
| RootedObject newIdentity(cx); |
| |
| { |
| AutoDisableProxyCheck adpc(cx->runtime()); |
| |
| JSCompartment* destination = target->compartment(); |
| |
| if (origobj->compartment() == destination) { |
| // If the original object is in the same compartment as the |
| // destination, then we know that we won't find a wrapper in the |
| // destination's cross compartment map and that the same |
| // object will continue to work. |
| if (!JSObject::swap(cx, origobj, target)) |
| MOZ_CRASH(); |
| newIdentity = origobj; |
| } else if (WrapperMap::Ptr p = destination->lookupWrapper(origv)) { |
| // There might already be a wrapper for the original object in |
| // the new compartment. If there is, we use its identity and swap |
| // in the contents of |target|. |
| newIdentity = &p->value().get().toObject(); |
| |
| // When we remove origv from the wrapper map, its wrapper, newIdentity, |
| // must immediately cease to be a cross-compartment wrapper. Neuter it. |
| destination->removeWrapper(p); |
| NukeCrossCompartmentWrapper(cx, newIdentity); |
| |
| if (!JSObject::swap(cx, newIdentity, target)) |
| MOZ_CRASH(); |
| } else { |
| // Otherwise, we use |target| for the new identity object. |
| newIdentity = target; |
| } |
| |
| // Now, iterate through other scopes looking for references to the |
| // old object, and update the relevant cross-compartment wrappers. |
| if (!RemapAllWrappersForObject(cx, origobj, newIdentity)) |
| MOZ_CRASH(); |
| |
| // Lastly, update the original object to point to the new one. |
| if (origobj->compartment() != destination) { |
| RootedObject newIdentityWrapper(cx, newIdentity); |
| AutoCompartment ac(cx, origobj); |
| if (!JS_WrapObject(cx, &newIdentityWrapper)) |
| MOZ_CRASH(); |
| MOZ_ASSERT(Wrapper::wrappedObject(newIdentityWrapper) == newIdentity); |
| if (!JSObject::swap(cx, origobj, newIdentityWrapper)) |
| MOZ_CRASH(); |
| origobj->compartment()->putWrapper(cx, CrossCompartmentKey(newIdentity), origv); |
| } |
| } |
| |
| // The new identity object might be one of several things. Return it to avoid |
| // ambiguity. |
| return newIdentity; |
| } |
| |
| /* |
| * Recompute all cross-compartment wrappers for an object, resetting state. |
| * Gecko uses this to clear Xray wrappers when doing a navigation that reuses |
| * the inner window and global object. |
| */ |
| JS_PUBLIC_API(bool) |
| JS_RefreshCrossCompartmentWrappers(JSContext* cx, HandleObject obj) |
| { |
| return RemapAllWrappersForObject(cx, obj, obj); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_InitStandardClasses(JSContext* cx, HandleObject obj) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| assertSameCompartment(cx, obj); |
| |
| Rooted<GlobalObject*> global(cx, &obj->global()); |
| return GlobalObject::initStandardClasses(cx, global); |
| } |
| |
| #define EAGER_ATOM(name) NAME_OFFSET(name) |
| |
| typedef struct JSStdName { |
| size_t atomOffset; /* offset of atom pointer in JSAtomState */ |
| JSProtoKey key; |
| bool isDummy() const { return key == JSProto_Null; } |
| bool isSentinel() const { return key == JSProto_LIMIT; } |
| } JSStdName; |
| |
| static const JSStdName* |
| LookupStdName(const JSAtomState& names, JSAtom* name, const JSStdName* table) |
| { |
| for (unsigned i = 0; !table[i].isSentinel(); i++) { |
| if (table[i].isDummy()) |
| continue; |
| JSAtom* atom = AtomStateOffsetToName(names, table[i].atomOffset); |
| MOZ_ASSERT(atom); |
| if (name == atom) |
| return &table[i]; |
| } |
| |
| return nullptr; |
| } |
| |
| /* |
| * Table of standard classes, indexed by JSProtoKey. For entries where the |
| * JSProtoKey does not correspond to a class with a meaningful constructor, we |
| * insert a null entry into the table. |
| */ |
| #define STD_NAME_ENTRY(name, code, init, clasp) { EAGER_ATOM(name), static_cast<JSProtoKey>(code) }, |
| #define STD_DUMMY_ENTRY(name, code, init, dummy) { 0, JSProto_Null }, |
| static const JSStdName standard_class_names[] = { |
| JS_FOR_PROTOTYPES(STD_NAME_ENTRY, STD_DUMMY_ENTRY) |
| { 0, JSProto_LIMIT } |
| }; |
| |
| /* |
| * Table of top-level function and constant names and the JSProtoKey of the |
| * standard class that initializes them. |
| */ |
| static const JSStdName builtin_property_names[] = { |
| { EAGER_ATOM(eval), JSProto_Object }, |
| |
| /* Global properties and functions defined by the Number class. */ |
| { EAGER_ATOM(NaN), JSProto_Number }, |
| { EAGER_ATOM(Infinity), JSProto_Number }, |
| { EAGER_ATOM(isNaN), JSProto_Number }, |
| { EAGER_ATOM(isFinite), JSProto_Number }, |
| { EAGER_ATOM(parseFloat), JSProto_Number }, |
| { EAGER_ATOM(parseInt), JSProto_Number }, |
| |
| /* String global functions. */ |
| { EAGER_ATOM(escape), JSProto_String }, |
| { EAGER_ATOM(unescape), JSProto_String }, |
| { EAGER_ATOM(decodeURI), JSProto_String }, |
| { EAGER_ATOM(encodeURI), JSProto_String }, |
| { EAGER_ATOM(decodeURIComponent), JSProto_String }, |
| { EAGER_ATOM(encodeURIComponent), JSProto_String }, |
| #if JS_HAS_UNEVAL |
| { EAGER_ATOM(uneval), JSProto_String }, |
| #endif |
| #ifdef ENABLE_BINARYDATA |
| { EAGER_ATOM(SIMD), JSProto_SIMD }, |
| { EAGER_ATOM(TypedObject), JSProto_TypedObject }, |
| #endif |
| #ifdef ENABLE_SHARED_ARRAY_BUFFER |
| { EAGER_ATOM(Atomics), JSProto_Atomics }, |
| #endif |
| |
| { 0, JSProto_LIMIT } |
| }; |
| |
| #undef EAGER_ATOM |
| |
| JS_PUBLIC_API(bool) |
| JS_ResolveStandardClass(JSContext* cx, HandleObject obj, HandleId id, bool* resolved) |
| { |
| JSRuntime* rt; |
| const JSStdName* stdnm; |
| |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id); |
| |
| Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>()); |
| *resolved = false; |
| |
| rt = cx->runtime(); |
| if (!rt->hasContexts() || !JSID_IS_ATOM(id)) |
| return true; |
| |
| /* Check whether we're resolving 'undefined', and define it if so. */ |
| JSAtom* idAtom = JSID_TO_ATOM(id); |
| JSAtom* undefinedAtom = cx->names().undefined; |
| if (idAtom == undefinedAtom) { |
| *resolved = true; |
| return DefineProperty(cx, global, id, UndefinedHandleValue, nullptr, nullptr, |
| JSPROP_PERMANENT | JSPROP_READONLY | JSPROP_RESOLVING); |
| } |
| |
| /* Try for class constructors/prototypes named by well-known atoms. */ |
| stdnm = LookupStdName(cx->names(), idAtom, standard_class_names); |
| |
| /* Try less frequently used top-level functions and constants. */ |
| if (!stdnm) |
| stdnm = LookupStdName(cx->names(), idAtom, builtin_property_names); |
| |
| // If this class is anonymous, then it doesn't exist as a global |
| // property, so we won't resolve anything. |
| JSProtoKey key = stdnm ? stdnm->key : JSProto_Null; |
| if (key != JSProto_Null) { |
| const Class* clasp = ProtoKeyToClass(key); |
| if (!clasp || !(clasp->flags & JSCLASS_IS_ANONYMOUS)) { |
| if (!GlobalObject::ensureConstructor(cx, global, key)) |
| return false; |
| |
| *resolved = true; |
| return true; |
| } |
| } |
| |
| // There is no such property to resolve. An ordinary resolve hook would |
| // just return true at this point. But the global object is special in one |
| // more way: its prototype chain is lazily initialized. That is, |
| // global->getProto() might be null right now because we haven't created |
| // Object.prototype yet. Force it now. |
| if (!global->getOrCreateObjectPrototype(cx)) |
| return false; |
| |
| return true; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_MayResolveStandardClass(const JSAtomState& names, jsid id, JSObject* maybeObj) |
| { |
| MOZ_ASSERT_IF(maybeObj, maybeObj->is<GlobalObject>()); |
| |
| // The global object's resolve hook is special: JS_ResolveStandardClass |
| // initializes the prototype chain lazily. Only attempt to optimize here |
| // if we know the prototype chain has been initialized. |
| if (!maybeObj || !maybeObj->getProto()) |
| return true; |
| |
| if (!JSID_IS_ATOM(id)) |
| return false; |
| |
| JSAtom* atom = JSID_TO_ATOM(id); |
| |
| return atom == names.undefined || |
| LookupStdName(names, atom, standard_class_names) || |
| LookupStdName(names, atom, builtin_property_names); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_EnumerateStandardClasses(JSContext* cx, HandleObject obj) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| MOZ_ASSERT(obj->is<GlobalObject>()); |
| Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>()); |
| return GlobalObject::initStandardClasses(cx, global); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetClassObject(JSContext* cx, JSProtoKey key, MutableHandleObject objp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| return GetBuiltinConstructor(cx, key, objp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetClassPrototype(JSContext* cx, JSProtoKey key, MutableHandleObject objp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| return GetBuiltinPrototype(cx, key, objp); |
| } |
| |
| namespace JS { |
| |
| JS_PUBLIC_API(void) |
| ProtoKeyToId(JSContext* cx, JSProtoKey key, MutableHandleId idp) |
| { |
| idp.set(NameToId(ClassName(key, cx))); |
| } |
| |
| } /* namespace JS */ |
| |
| JS_PUBLIC_API(JSProtoKey) |
| JS_IdToProtoKey(JSContext* cx, HandleId id) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| if (!JSID_IS_ATOM(id)) |
| return JSProto_Null; |
| |
| JSAtom* atom = JSID_TO_ATOM(id); |
| const JSStdName* stdnm = LookupStdName(cx->names(), atom, standard_class_names); |
| if (!stdnm) |
| return JSProto_Null; |
| |
| MOZ_ASSERT(MOZ_ARRAY_LENGTH(standard_class_names) == JSProto_LIMIT + 1); |
| return static_cast<JSProtoKey>(stdnm - standard_class_names); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetObjectPrototype(JSContext* cx, HandleObject forObj) |
| { |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, forObj); |
| return forObj->global().getOrCreateObjectPrototype(cx); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetFunctionPrototype(JSContext* cx, HandleObject forObj) |
| { |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, forObj); |
| return forObj->global().getOrCreateFunctionPrototype(cx); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetArrayPrototype(JSContext* cx, HandleObject forObj) |
| { |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, forObj); |
| Rooted<GlobalObject*> global(cx, &forObj->global()); |
| return GlobalObject::getOrCreateArrayPrototype(cx, global); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetErrorPrototype(JSContext* cx) |
| { |
| CHECK_REQUEST(cx); |
| Rooted<GlobalObject*> global(cx, cx->global()); |
| return GlobalObject::getOrCreateCustomErrorPrototype(cx, global, JSEXN_ERR); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetIteratorPrototype(JSContext* cx) |
| { |
| CHECK_REQUEST(cx); |
| Rooted<GlobalObject*> global(cx, cx->global()); |
| return GlobalObject::getOrCreateIteratorPrototype(cx, global); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetGlobalForObject(JSContext* cx, JSObject* obj) |
| { |
| AssertHeapIsIdle(cx); |
| assertSameCompartment(cx, obj); |
| return &obj->global(); |
| } |
| |
| extern JS_PUBLIC_API(bool) |
| JS_IsGlobalObject(JSObject* obj) |
| { |
| return obj->is<GlobalObject>(); |
| } |
| |
| extern JS_PUBLIC_API(JSObject*) |
| JS_GlobalLexicalScope(JSObject* obj) |
| { |
| return &obj->as<GlobalObject>().lexicalScope(); |
| } |
| |
| extern JS_PUBLIC_API(bool) |
| JS_HasExtensibleLexicalScope(JSObject* obj) |
| { |
| return obj->is<GlobalObject>() || obj->compartment()->getNonSyntacticLexicalScope(obj); |
| } |
| |
| extern JS_PUBLIC_API(JSObject*) |
| JS_ExtensibleLexicalScope(JSObject* obj) |
| { |
| JSObject* lexical = nullptr; |
| if (obj->is<GlobalObject>()) |
| lexical = JS_GlobalLexicalScope(obj); |
| else |
| lexical = obj->compartment()->getNonSyntacticLexicalScope(obj); |
| MOZ_ASSERT(lexical); |
| return lexical; |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetGlobalForCompartmentOrNull(JSContext* cx, JSCompartment* c) |
| { |
| AssertHeapIsIdleOrIterating(cx); |
| assertSameCompartment(cx, c); |
| return c->maybeGlobal(); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS::CurrentGlobalOrNull(JSContext* cx) |
| { |
| AssertHeapIsIdleOrIterating(cx); |
| CHECK_REQUEST(cx); |
| if (!cx->compartment()) |
| return nullptr; |
| return cx->global(); |
| } |
| |
| JS_PUBLIC_API(Value) |
| JS_ComputeThis(JSContext* cx, Value* vp) |
| { |
| AssertHeapIsIdle(cx); |
| assertSameCompartment(cx, JSValueArray(vp, 2)); |
| CallReceiver call = CallReceiverFromVp(vp); |
| if (!BoxNonStrictThis(cx, call)) |
| return NullValue(); |
| return call.thisv(); |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_malloc(JSContext* cx, size_t nbytes) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| return static_cast<void*>(cx->runtime()->pod_malloc<uint8_t>(nbytes)); |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_realloc(JSContext* cx, void* p, size_t oldBytes, size_t newBytes) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| return static_cast<void*>(cx->zone()->pod_realloc<uint8_t>(static_cast<uint8_t*>(p), oldBytes, |
| newBytes)); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_free(JSContext* cx, void* p) |
| { |
| return js_free(p); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_freeop(JSFreeOp* fop, void* p) |
| { |
| return FreeOp::get(fop)->free_(p); |
| } |
| |
| JS_PUBLIC_API(JSFreeOp*) |
| JS_GetDefaultFreeOp(JSRuntime* rt) |
| { |
| return rt->defaultFreeOp(); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_updateMallocCounter(JSContext* cx, size_t nbytes) |
| { |
| return cx->runtime()->updateMallocCounter(cx->zone(), nbytes); |
| } |
| |
| JS_PUBLIC_API(char*) |
| JS_strdup(JSContext* cx, const char* s) |
| { |
| AssertHeapIsIdle(cx); |
| return DuplicateString(cx, s).release(); |
| } |
| |
| JS_PUBLIC_API(char*) |
| JS_strdup(JSRuntime* rt, const char* s) |
| { |
| AssertHeapIsIdle(rt); |
| size_t n = strlen(s) + 1; |
| char* p = rt->pod_malloc<char>(n); |
| if (!p) |
| return nullptr; |
| return static_cast<char*>(js_memcpy(p, s, n)); |
| } |
| |
| #undef JS_AddRoot |
| |
| JS_PUBLIC_API(bool) |
| JS_AddExtraGCRootsTracer(JSRuntime* rt, JSTraceDataOp traceOp, void* data) |
| { |
| return rt->gc.addBlackRootsTracer(traceOp, data); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_RemoveExtraGCRootsTracer(JSRuntime* rt, JSTraceDataOp traceOp, void* data) |
| { |
| return rt->gc.removeBlackRootsTracer(traceOp, data); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_GC(JSRuntime* rt) |
| { |
| AssertHeapIsIdle(rt); |
| JS::PrepareForFullGC(rt); |
| rt->gc.gc(GC_NORMAL, JS::gcreason::API); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_MaybeGC(JSContext* cx) |
| { |
| GCRuntime& gc = cx->runtime()->gc; |
| if (!gc.maybeGC(cx->zone())) |
| gc.maybePeriodicFullGC(); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetGCCallback(JSRuntime* rt, JSGCCallback cb, void* data) |
| { |
| AssertHeapIsIdle(rt); |
| rt->gc.setGCCallback(cb, data); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_AddFinalizeCallback(JSRuntime* rt, JSFinalizeCallback cb, void* data) |
| { |
| AssertHeapIsIdle(rt); |
| return rt->gc.addFinalizeCallback(cb, data); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_RemoveFinalizeCallback(JSRuntime* rt, JSFinalizeCallback cb) |
| { |
| rt->gc.removeFinalizeCallback(cb); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_AddWeakPointerZoneGroupCallback(JSRuntime* rt, JSWeakPointerZoneGroupCallback cb, void* data) |
| { |
| AssertHeapIsIdle(rt); |
| return rt->gc.addWeakPointerZoneGroupCallback(cb, data); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_RemoveWeakPointerZoneGroupCallback(JSRuntime* rt, JSWeakPointerZoneGroupCallback cb) |
| { |
| rt->gc.removeWeakPointerZoneGroupCallback(cb); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_AddWeakPointerCompartmentCallback(JSRuntime* rt, JSWeakPointerCompartmentCallback cb, |
| void* data) |
| { |
| AssertHeapIsIdle(rt); |
| return rt->gc.addWeakPointerCompartmentCallback(cb, data); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_RemoveWeakPointerCompartmentCallback(JSRuntime* rt, JSWeakPointerCompartmentCallback cb) |
| { |
| rt->gc.removeWeakPointerCompartmentCallback(cb); |
| } |
| |
| |
| JS_PUBLIC_API(void) |
| JS_UpdateWeakPointerAfterGC(JS::Heap<JSObject*>* objp) |
| { |
| JS_UpdateWeakPointerAfterGCUnbarriered(objp->unsafeGet()); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_UpdateWeakPointerAfterGCUnbarriered(JSObject** objp) |
| { |
| if (IsAboutToBeFinalizedUnbarriered(objp)) |
| *objp = nullptr; |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetGCParameter(JSRuntime* rt, JSGCParamKey key, uint32_t value) |
| { |
| AutoLockGC lock(rt); |
| rt->gc.setParameter(key, value, lock); |
| } |
| |
| JS_PUBLIC_API(uint32_t) |
| JS_GetGCParameter(JSRuntime* rt, JSGCParamKey key) |
| { |
| AutoLockGC lock(rt); |
| return rt->gc.getParameter(key, lock); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetGCParameterForThread(JSContext* cx, JSGCParamKey key, uint32_t value) |
| { |
| MOZ_ASSERT(key == JSGC_MAX_CODE_CACHE_BYTES); |
| } |
| |
| JS_PUBLIC_API(uint32_t) |
| JS_GetGCParameterForThread(JSContext* cx, JSGCParamKey key) |
| { |
| MOZ_ASSERT(key == JSGC_MAX_CODE_CACHE_BYTES); |
| return 0; |
| } |
| |
| static const size_t NumGCConfigs = 14; |
| struct JSGCConfig { |
| JSGCParamKey key; |
| uint32_t value; |
| }; |
| |
| JS_PUBLIC_API(void) |
| JS_SetGCParametersBasedOnAvailableMemory(JSRuntime* rt, uint32_t availMem) |
| { |
| static const JSGCConfig minimal[NumGCConfigs] = { |
| {JSGC_MAX_MALLOC_BYTES, 6 * 1024 * 1024}, |
| {JSGC_SLICE_TIME_BUDGET, 30}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_HIGH_FREQUENCY_HIGH_LIMIT, 40}, |
| {JSGC_HIGH_FREQUENCY_LOW_LIMIT, 0}, |
| {JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MAX, 300}, |
| {JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MIN, 120}, |
| {JSGC_LOW_FREQUENCY_HEAP_GROWTH, 120}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_ALLOCATION_THRESHOLD, 1}, |
| {JSGC_DECOMMIT_THRESHOLD, 1}, |
| {JSGC_MODE, JSGC_MODE_INCREMENTAL} |
| }; |
| |
| const JSGCConfig* config = minimal; |
| if (availMem > 512) { |
| static const JSGCConfig nominal[NumGCConfigs] = { |
| {JSGC_MAX_MALLOC_BYTES, 6 * 1024 * 1024}, |
| {JSGC_SLICE_TIME_BUDGET, 30}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1000}, |
| {JSGC_HIGH_FREQUENCY_HIGH_LIMIT, 500}, |
| {JSGC_HIGH_FREQUENCY_LOW_LIMIT, 100}, |
| {JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MAX, 300}, |
| {JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MIN, 150}, |
| {JSGC_LOW_FREQUENCY_HEAP_GROWTH, 150}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_HIGH_FREQUENCY_TIME_LIMIT, 1500}, |
| {JSGC_ALLOCATION_THRESHOLD, 30}, |
| {JSGC_DECOMMIT_THRESHOLD, 32}, |
| {JSGC_MODE, JSGC_MODE_COMPARTMENT} |
| }; |
| |
| config = nominal; |
| } |
| |
| for (size_t i = 0; i < NumGCConfigs; i++) |
| JS_SetGCParameter(rt, config[i].key, config[i].value); |
| } |
| |
| |
| JS_PUBLIC_API(JSString*) |
| JS_NewExternalString(JSContext* cx, const char16_t* chars, size_t length, |
| const JSStringFinalizer* fin) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| JSString* s = JSExternalString::new_(cx, chars, length, fin); |
| return s; |
| } |
| |
| extern JS_PUBLIC_API(bool) |
| JS_IsExternalString(JSString* str) |
| { |
| return str->isExternal(); |
| } |
| |
| extern JS_PUBLIC_API(const JSStringFinalizer*) |
| JS_GetExternalStringFinalizer(JSString* str) |
| { |
| return str->asExternal().externalFinalizer(); |
| } |
| |
| static void |
| SetNativeStackQuotaAndLimit(JSRuntime* rt, StackKind kind, size_t stackSize) |
| { |
| rt->nativeStackQuota[kind] = stackSize; |
| |
| #if JS_STACK_GROWTH_DIRECTION > 0 |
| if (stackSize == 0) { |
| rt->mainThread.nativeStackLimit[kind] = UINTPTR_MAX; |
| } else { |
| MOZ_ASSERT(rt->nativeStackBase <= size_t(-1) - stackSize); |
| rt->mainThread.nativeStackLimit[kind] = rt->nativeStackBase + stackSize - 1; |
| } |
| #else |
| if (stackSize == 0) { |
| rt->mainThread.nativeStackLimit[kind] = 0; |
| } else { |
| MOZ_ASSERT(rt->nativeStackBase >= stackSize); |
| rt->mainThread.nativeStackLimit[kind] = rt->nativeStackBase - (stackSize - 1); |
| } |
| #endif |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetNativeStackQuota(JSRuntime* rt, size_t systemCodeStackSize, size_t trustedScriptStackSize, |
| size_t untrustedScriptStackSize) |
| { |
| MOZ_ASSERT(rt->requestDepth == 0); |
| |
| if (!trustedScriptStackSize) |
| trustedScriptStackSize = systemCodeStackSize; |
| else |
| MOZ_ASSERT(trustedScriptStackSize < systemCodeStackSize); |
| |
| if (!untrustedScriptStackSize) |
| untrustedScriptStackSize = trustedScriptStackSize; |
| else |
| MOZ_ASSERT(untrustedScriptStackSize < trustedScriptStackSize); |
| |
| SetNativeStackQuotaAndLimit(rt, StackForSystemCode, systemCodeStackSize); |
| SetNativeStackQuotaAndLimit(rt, StackForTrustedScript, trustedScriptStackSize); |
| SetNativeStackQuotaAndLimit(rt, StackForUntrustedScript, untrustedScriptStackSize); |
| |
| rt->initJitStackLimit(); |
| } |
| |
| /************************************************************************/ |
| |
| JS_PUBLIC_API(bool) |
| JS_ValueToId(JSContext* cx, HandleValue value, MutableHandleId idp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, value); |
| return ValueToId<CanGC>(cx, value, idp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_StringToId(JSContext* cx, HandleString string, MutableHandleId idp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, string); |
| RootedValue value(cx, StringValue(string)); |
| return ValueToId<CanGC>(cx, value, idp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_IdToValue(JSContext* cx, jsid id, MutableHandleValue vp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| vp.set(IdToValue(id)); |
| assertSameCompartment(cx, vp); |
| return true; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::ToPrimitive(JSContext* cx, HandleObject obj, JSType hint, MutableHandleValue vp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| MOZ_ASSERT(obj != nullptr); |
| MOZ_ASSERT(hint == JSTYPE_VOID || hint == JSTYPE_STRING || hint == JSTYPE_NUMBER); |
| vp.setObject(*obj); |
| return ToPrimitiveSlow(cx, hint, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::GetFirstArgumentAsTypeHint(JSContext* cx, CallArgs args, JSType *result) |
| { |
| if (!args.get(0).isString()) { |
| JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NOT_EXPECTED_TYPE, |
| "Symbol.toPrimitive", |
| "\"string\", \"number\", or \"default\"", |
| InformalValueTypeName(args.get(0))); |
| return false; |
| } |
| |
| RootedString str(cx, args.get(0).toString()); |
| bool match; |
| |
| if (!EqualStrings(cx, str, cx->names().default_, &match)) |
| return false; |
| if (match) { |
| *result = JSTYPE_VOID; |
| return true; |
| } |
| |
| if (!EqualStrings(cx, str, cx->names().string, &match)) |
| return false; |
| if (match) { |
| *result = JSTYPE_STRING; |
| return true; |
| } |
| |
| if (!EqualStrings(cx, str, cx->names().number, &match)) |
| return false; |
| if (match) { |
| *result = JSTYPE_NUMBER; |
| return true; |
| } |
| |
| JSAutoByteString bytes; |
| JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NOT_EXPECTED_TYPE, |
| "Symbol.toPrimitive", |
| "\"string\", \"number\", or \"default\"", |
| ValueToSourceForError(cx, args.get(0), bytes)); |
| return false; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_PropertyStub(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp) |
| { |
| return true; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_StrictPropertyStub(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp, |
| ObjectOpResult& result) |
| { |
| return result.succeed(); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_InitClass(JSContext* cx, HandleObject obj, HandleObject parent_proto, |
| const JSClass* clasp, JSNative constructor, unsigned nargs, |
| const JSPropertySpec* ps, const JSFunctionSpec* fs, |
| const JSPropertySpec* static_ps, const JSFunctionSpec* static_fs) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, parent_proto); |
| return InitClass(cx, obj, parent_proto, Valueify(clasp), constructor, |
| nargs, ps, fs, static_ps, static_fs); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_LinkConstructorAndPrototype(JSContext* cx, HandleObject ctor, HandleObject proto) |
| { |
| return LinkConstructorAndPrototype(cx, ctor, proto); |
| } |
| |
| JS_PUBLIC_API(const JSClass*) |
| JS_GetClass(JSObject* obj) |
| { |
| return obj->getJSClass(); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_InstanceOf(JSContext* cx, HandleObject obj, const JSClass* clasp, CallArgs* args) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| #ifdef DEBUG |
| if (args) { |
| assertSameCompartment(cx, obj); |
| assertSameCompartment(cx, args->thisv(), args->calleev()); |
| } |
| #endif |
| if (!obj || obj->getJSClass() != clasp) { |
| if (args) |
| ReportIncompatibleMethod(cx, *args, Valueify(clasp)); |
| return false; |
| } |
| return true; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasInstance(JSContext* cx, HandleObject obj, HandleValue value, bool* bp) |
| { |
| AssertHeapIsIdle(cx); |
| assertSameCompartment(cx, obj, value); |
| return HasInstance(cx, obj, value, bp); |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetPrivate(JSObject* obj) |
| { |
| /* This function can be called by a finalizer. */ |
| return obj->as<NativeObject>().getPrivate(); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_SetPrivate(JSObject* obj, void* data) |
| { |
| /* This function can be called by a finalizer. */ |
| obj->as<NativeObject>().setPrivate(data); |
| } |
| |
| JS_PUBLIC_API(void*) |
| JS_GetInstancePrivate(JSContext* cx, HandleObject obj, const JSClass* clasp, CallArgs* args) |
| { |
| if (!JS_InstanceOf(cx, obj, clasp, args)) |
| return nullptr; |
| return obj->as<NativeObject>().getPrivate(); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_GetConstructor(JSContext* cx, HandleObject proto) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, proto); |
| |
| RootedValue cval(cx); |
| if (!GetProperty(cx, proto, proto, cx->names().constructor, &cval)) |
| return nullptr; |
| if (!IsFunctionObject(cval)) { |
| JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_NO_CONSTRUCTOR, |
| proto->getClass()->name); |
| return nullptr; |
| } |
| return &cval.toObject(); |
| } |
| |
| bool |
| JS::CompartmentOptions::extraWarnings(JSRuntime* rt) const |
| { |
| return extraWarningsOverride_.get(rt->options().extraWarnings()); |
| } |
| |
| bool |
| JS::CompartmentOptions::extraWarnings(JSContext* cx) const |
| { |
| return extraWarnings(cx->runtime()); |
| } |
| |
| JS::CompartmentOptions& |
| JS::CompartmentOptions::setZone(ZoneSpecifier spec) |
| { |
| zone_.spec = spec; |
| return *this; |
| } |
| |
| JS::CompartmentOptions& |
| JS::CompartmentOptions::setSameZoneAs(JSObject* obj) |
| { |
| zone_.pointer = static_cast<void*>(obj->zone()); |
| return *this; |
| } |
| |
| JS::CompartmentOptions& |
| JS::CompartmentOptionsRef(JSCompartment* compartment) |
| { |
| return compartment->options(); |
| } |
| |
| JS::CompartmentOptions& |
| JS::CompartmentOptionsRef(JSObject* obj) |
| { |
| return obj->compartment()->options(); |
| } |
| |
| JS::CompartmentOptions& |
| JS::CompartmentOptionsRef(JSContext* cx) |
| { |
| return cx->compartment()->options(); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_NewGlobalObject(JSContext* cx, const JSClass* clasp, JSPrincipals* principals, |
| JS::OnNewGlobalHookOption hookOption, |
| const JS::CompartmentOptions& options /* = JS::CompartmentOptions() */) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| return GlobalObject::new_(cx, Valueify(clasp), principals, hookOption, options); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_GlobalObjectTraceHook(JSTracer* trc, JSObject* global) |
| { |
| MOZ_ASSERT(global->is<GlobalObject>()); |
| |
| // Off thread parsing and compilation tasks create a dummy global which is then |
| // merged back into the host compartment. Since it used to be a global, it will still |
| // have this trace hook, but it does not have a meaning relative to its new compartment. |
| // We can safely skip it. |
| if (!global->isOwnGlobal()) |
| return; |
| |
| // Trace the compartment for any GC things that should only stick around if we know the |
| // compartment is live. |
| global->compartment()->trace(trc); |
| |
| JSTraceOp trace = global->compartment()->options().getTrace(); |
| if (trace) |
| trace(trc, global); |
| } |
| |
| JS_PUBLIC_API(void) |
| JS_FireOnNewGlobalObject(JSContext* cx, JS::HandleObject global) |
| { |
| // This hook is infallible, because we don't really want arbitrary script |
| // to be able to throw errors during delicate global creation routines. |
| // This infallibility will eat OOM and slow script, but if that happens |
| // we'll likely run up into them again soon in a fallible context. |
| Rooted<js::GlobalObject*> globalObject(cx, &global->as<GlobalObject>()); |
| Debugger::onNewGlobalObject(cx, globalObject); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_NewObject(JSContext* cx, const JSClass* jsclasp) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| const Class* clasp = Valueify(jsclasp); |
| if (!clasp) |
| clasp = &PlainObject::class_; /* default class is Object */ |
| |
| MOZ_ASSERT(clasp != &JSFunction::class_); |
| MOZ_ASSERT(!(clasp->flags & JSCLASS_IS_GLOBAL)); |
| |
| return NewObjectWithClassProto(cx, clasp, nullptr); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_NewObjectWithGivenProto(JSContext* cx, const JSClass* jsclasp, HandleObject proto) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, proto); |
| |
| const Class* clasp = Valueify(jsclasp); |
| if (!clasp) |
| clasp = &PlainObject::class_; /* default class is Object */ |
| |
| MOZ_ASSERT(clasp != &JSFunction::class_); |
| MOZ_ASSERT(!(clasp->flags & JSCLASS_IS_GLOBAL)); |
| |
| return NewObjectWithGivenProto(cx, clasp, proto); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_NewPlainObject(JSContext* cx) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| return NewBuiltinClassInstance<PlainObject>(cx); |
| } |
| |
| JS_PUBLIC_API(JSObject*) |
| JS_NewObjectForConstructor(JSContext* cx, const JSClass* clasp, const CallArgs& args) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| Value callee = args.calleev(); |
| assertSameCompartment(cx, callee); |
| RootedObject obj(cx, &callee.toObject()); |
| return CreateThis(cx, Valueify(clasp), obj); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_IsNative(JSObject* obj) |
| { |
| return obj->isNative(); |
| } |
| |
| JS_PUBLIC_API(JSRuntime*) |
| JS_GetObjectRuntime(JSObject* obj) |
| { |
| return obj->compartment()->runtimeFromMainThread(); |
| } |
| |
| |
| /*** Standard internal methods *******************************************************************/ |
| |
| JS_PUBLIC_API(bool) |
| JS_GetPrototype(JSContext* cx, HandleObject obj, MutableHandleObject result) |
| { |
| return GetPrototype(cx, obj, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetPrototype(JSContext* cx, HandleObject obj, HandleObject proto) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, proto); |
| |
| return SetPrototype(cx, obj, proto); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_IsExtensible(JSContext* cx, HandleObject obj, bool* extensible) |
| { |
| return IsExtensible(cx, obj, extensible); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_PreventExtensions(JSContext* cx, JS::HandleObject obj, ObjectOpResult& result) |
| { |
| return PreventExtensions(cx, obj, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetImmutablePrototype(JSContext *cx, JS::HandleObject obj, bool *succeeded) |
| { |
| return SetImmutablePrototype(cx, obj, succeeded); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetOwnPropertyDescriptorById(JSContext* cx, HandleObject obj, HandleId id, |
| MutableHandle<JSPropertyDescriptor> desc) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| return GetOwnPropertyDescriptor(cx, obj, id, desc); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetOwnPropertyDescriptor(JSContext* cx, HandleObject obj, const char* name, |
| MutableHandle<JSPropertyDescriptor> desc) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_GetOwnPropertyDescriptorById(cx, obj, id, desc); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetOwnUCPropertyDescriptor(JSContext* cx, HandleObject obj, const char16_t* name, |
| MutableHandle<JSPropertyDescriptor> desc) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, js_strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_GetOwnPropertyDescriptorById(cx, obj, id, desc); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetPropertyDescriptorById(JSContext* cx, HandleObject obj, HandleId id, |
| MutableHandle<JSPropertyDescriptor> desc) |
| { |
| return GetPropertyDescriptor(cx, obj, id, desc); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetPropertyDescriptor(JSContext* cx, HandleObject obj, const char* name, |
| MutableHandle<JSPropertyDescriptor> desc) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return atom && JS_GetPropertyDescriptorById(cx, obj, id, desc); |
| } |
| |
| static bool |
| DefinePropertyByDescriptor(JSContext* cx, HandleObject obj, HandleId id, |
| Handle<JSPropertyDescriptor> desc, ObjectOpResult& result) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id, desc); |
| return DefineProperty(cx, obj, id, desc.value(), desc.getter(), desc.setter(), |
| desc.attributes(), result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, |
| Handle<JSPropertyDescriptor> desc, ObjectOpResult& result) |
| { |
| return DefinePropertyByDescriptor(cx, obj, id, desc, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, |
| Handle<JSPropertyDescriptor> desc) |
| { |
| ObjectOpResult result; |
| return DefinePropertyByDescriptor(cx, obj, id, desc, result) && |
| result.checkStrict(cx, obj, id); |
| } |
| |
| static bool |
| DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, HandleValue value, |
| const JSNativeWrapper& get, const JSNativeWrapper& set, |
| unsigned attrs, unsigned flags) |
| { |
| JSGetterOp getter = JS_CAST_NATIVE_TO(get.op, JSGetterOp); |
| JSSetterOp setter = JS_CAST_NATIVE_TO(set.op, JSSetterOp); |
| |
| // JSPROP_READONLY has no meaning when accessors are involved. Ideally we'd |
| // throw if this happens, but we've accepted it for long enough that it's |
| // not worth trying to make callers change their ways. Just flip it off on |
| // its way through the API layer so that we can enforce this internally. |
| if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) |
| attrs &= ~JSPROP_READONLY; |
| |
| // When we use DefineProperty, we need full scriptable Function objects rather |
| // than JSNatives. However, we might be pulling this property descriptor off |
| // of something with JSNative property descriptors. If we are, wrap them in |
| // JS Function objects. |
| // |
| // But skip doing this if our accessors are the well-known stub |
| // accessors, since those are known to be JSGetterOps. Assert |
| // some sanity about it, though. |
| MOZ_ASSERT_IF(getter == JS_PropertyStub, |
| setter == JS_StrictPropertyStub || (attrs & JSPROP_PROPOP_ACCESSORS)); |
| MOZ_ASSERT_IF(setter == JS_StrictPropertyStub, |
| getter == JS_PropertyStub || (attrs & JSPROP_PROPOP_ACCESSORS)); |
| |
| // If !(attrs & JSPROP_PROPOP_ACCESSORS), then either getter/setter are both |
| // possibly-null JSNatives (or possibly-null JSFunction* if JSPROP_GETTER or |
| // JSPROP_SETTER is appropriately set), or both are the well-known property |
| // stubs. The subsequent block must handle only the first of these cases, |
| // so carefully exclude the latter case. |
| if (!(attrs & JSPROP_PROPOP_ACCESSORS) && |
| getter != JS_PropertyStub && setter != JS_StrictPropertyStub) |
| { |
| RootedAtom atom(cx, JSID_IS_ATOM(id) ? JSID_TO_ATOM(id) : nullptr); |
| if (getter && !(attrs & JSPROP_GETTER)) { |
| JSFunction* getobj = NewNativeFunction(cx, (Native) getter, 0, atom); |
| if (!getobj) |
| return false; |
| |
| if (get.info) |
| getobj->setJitInfo(get.info); |
| |
| getter = JS_DATA_TO_FUNC_PTR(GetterOp, getobj); |
| attrs |= JSPROP_GETTER; |
| } |
| if (setter && !(attrs & JSPROP_SETTER)) { |
| // Root just the getter, since the setter is not yet a JSObject. |
| AutoRooterGetterSetter getRoot(cx, JSPROP_GETTER, &getter, nullptr); |
| JSFunction* setobj = NewNativeFunction(cx, (Native) setter, 1, atom); |
| if (!setobj) |
| return false; |
| |
| if (set.info) |
| setobj->setJitInfo(set.info); |
| |
| setter = JS_DATA_TO_FUNC_PTR(SetterOp, setobj); |
| attrs |= JSPROP_SETTER; |
| } |
| } else { |
| attrs &= ~JSPROP_PROPOP_ACCESSORS; |
| } |
| |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id, value, |
| (attrs & JSPROP_GETTER) |
| ? JS_FUNC_TO_DATA_PTR(JSObject*, getter) |
| : nullptr, |
| (attrs & JSPROP_SETTER) |
| ? JS_FUNC_TO_DATA_PTR(JSObject*, setter) |
| : nullptr); |
| |
| // In most places throughout the engine, a property with null getter and |
| // not JSPROP_GETTER/SETTER/SHARED has no getter, and the same for setters: |
| // it's just a plain old data property. However the JS_Define* APIs use |
| // null getter and setter to mean "default to the Class getProperty and |
| // setProperty ops". |
| if (!(attrs & (JSPROP_GETTER | JSPROP_SETTER))) { |
| if (!getter) |
| getter = obj->getClass()->getProperty; |
| if (!setter) |
| setter = obj->getClass()->setProperty; |
| } |
| if (getter == JS_PropertyStub) |
| getter = nullptr; |
| if (setter == JS_StrictPropertyStub) |
| setter = nullptr; |
| return DefineProperty(cx, obj, id, value, getter, setter, attrs); |
| } |
| |
| /* |
| * Wrapper functions to create wrappers with no corresponding JSJitInfo from API |
| * function arguments. |
| */ |
| static JSNativeWrapper |
| NativeOpWrapper(Native native) |
| { |
| JSNativeWrapper ret; |
| ret.op = native; |
| ret.info = nullptr; |
| return ret; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, HandleValue value, |
| unsigned attrs, Native getter, Native setter) |
| { |
| return DefinePropertyById(cx, obj, id, value, |
| NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, HandleObject valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| RootedValue value(cx, ObjectValue(*valueArg)); |
| return DefinePropertyById(cx, obj, id, value, |
| NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, HandleString valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| RootedValue value(cx, StringValue(valueArg)); |
| return DefinePropertyById(cx, obj, id, value, |
| NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, int32_t valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| Value value = Int32Value(valueArg); |
| return DefinePropertyById(cx, obj, id, HandleValue::fromMarkedLocation(&value), |
| NativeOpWrapper(getter), NativeOpWrapper(setter), attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, uint32_t valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| Value value = NumberValue(valueArg); |
| return DefinePropertyById(cx, obj, id, HandleValue::fromMarkedLocation(&value), |
| NativeOpWrapper(getter), NativeOpWrapper(setter), attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefinePropertyById(JSContext* cx, HandleObject obj, HandleId id, double valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| Value value = NumberValue(valueArg); |
| return DefinePropertyById(cx, obj, id, HandleValue::fromMarkedLocation(&value), |
| NativeOpWrapper(getter), NativeOpWrapper(setter), attrs, 0); |
| } |
| |
| static bool |
| DefineProperty(JSContext* cx, HandleObject obj, const char* name, HandleValue value, |
| const JSNativeWrapper& getter, const JSNativeWrapper& setter, |
| unsigned attrs, unsigned flags) |
| { |
| AutoRooterGetterSetter gsRoot(cx, attrs, const_cast<JSNative*>(&getter.op), |
| const_cast<JSNative*>(&setter.op)); |
| |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| |
| return DefinePropertyById(cx, obj, id, value, getter, setter, attrs, flags); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineProperty(JSContext* cx, HandleObject obj, const char* name, HandleValue value, |
| unsigned attrs, |
| Native getter /* = nullptr */, Native setter /* = nullptr */) |
| { |
| return DefineProperty(cx, obj, name, value, NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineProperty(JSContext* cx, HandleObject obj, const char* name, HandleObject valueArg, |
| unsigned attrs, |
| Native getter /* = nullptr */, Native setter /* = nullptr */) |
| { |
| RootedValue value(cx, ObjectValue(*valueArg)); |
| return DefineProperty(cx, obj, name, value, NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineProperty(JSContext* cx, HandleObject obj, const char* name, HandleString valueArg, |
| unsigned attrs, |
| Native getter /* = nullptr */, Native setter /* = nullptr */) |
| { |
| RootedValue value(cx, StringValue(valueArg)); |
| return DefineProperty(cx, obj, name, value, NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineProperty(JSContext* cx, HandleObject obj, const char* name, int32_t valueArg, |
| unsigned attrs, |
| Native getter /* = nullptr */, Native setter /* = nullptr */) |
| { |
| Value value = Int32Value(valueArg); |
| return DefineProperty(cx, obj, name, HandleValue::fromMarkedLocation(&value), |
| NativeOpWrapper(getter), NativeOpWrapper(setter), attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineProperty(JSContext* cx, HandleObject obj, const char* name, uint32_t valueArg, |
| unsigned attrs, |
| Native getter /* = nullptr */, Native setter /* = nullptr */) |
| { |
| Value value = NumberValue(valueArg); |
| return DefineProperty(cx, obj, name, HandleValue::fromMarkedLocation(&value), |
| NativeOpWrapper(getter), NativeOpWrapper(setter), attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineProperty(JSContext* cx, HandleObject obj, const char* name, double valueArg, |
| unsigned attrs, |
| Native getter /* = nullptr */, Native setter /* = nullptr */) |
| { |
| Value value = NumberValue(valueArg); |
| return DefineProperty(cx, obj, name, HandleValue::fromMarkedLocation(&value), |
| NativeOpWrapper(getter), NativeOpWrapper(setter), attrs, 0); |
| } |
| |
| #define AUTO_NAMELEN(s,n) (((n) == (size_t)-1) ? js_strlen(s) : (n)) |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| Handle<JSPropertyDescriptor> desc, |
| ObjectOpResult& result) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return DefinePropertyByDescriptor(cx, obj, id, desc, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| Handle<JSPropertyDescriptor> desc) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| ObjectOpResult result; |
| return DefinePropertyByDescriptor(cx, obj, id, desc, result) && |
| result.checkStrict(cx, obj, id); |
| } |
| |
| static bool |
| DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| const Value& value_, Native getter, Native setter, unsigned attrs, |
| unsigned flags) |
| { |
| RootedValue value(cx, value_); |
| AutoRooterGetterSetter gsRoot(cx, attrs, &getter, &setter); |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return DefinePropertyById(cx, obj, id, value, NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, flags); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| HandleValue value, unsigned attrs, Native getter, Native setter) |
| { |
| return DefineUCProperty(cx, obj, name, namelen, value, getter, setter, attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| HandleObject valueArg, unsigned attrs, Native getter, Native setter) |
| { |
| RootedValue value(cx, ObjectValue(*valueArg)); |
| return DefineUCProperty(cx, obj, name, namelen, value, getter, setter, attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| HandleString valueArg, unsigned attrs, Native getter, Native setter) |
| { |
| RootedValue value(cx, StringValue(valueArg)); |
| return DefineUCProperty(cx, obj, name, namelen, value, getter, setter, attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| int32_t valueArg, unsigned attrs, Native getter, Native setter) |
| { |
| Value value = Int32Value(valueArg); |
| return DefineUCProperty(cx, obj, name, namelen, HandleValue::fromMarkedLocation(&value), |
| getter, setter, attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| uint32_t valueArg, unsigned attrs, Native getter, Native setter) |
| { |
| Value value = NumberValue(valueArg); |
| return DefineUCProperty(cx, obj, name, namelen, HandleValue::fromMarkedLocation(&value), |
| getter, setter, attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| double valueArg, unsigned attrs, Native getter, Native setter) |
| { |
| Value value = NumberValue(valueArg); |
| return DefineUCProperty(cx, obj, name, namelen, HandleValue::fromMarkedLocation(&value), |
| getter, setter, attrs, 0); |
| } |
| |
| static bool |
| DefineElement(JSContext* cx, HandleObject obj, uint32_t index, HandleValue value, |
| unsigned attrs, Native getter, Native setter) |
| { |
| AutoRooterGetterSetter gsRoot(cx, attrs, &getter, &setter); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| RootedId id(cx); |
| if (!IndexToId(cx, index, &id)) |
| return false; |
| return DefinePropertyById(cx, obj, id, value, |
| NativeOpWrapper(getter), NativeOpWrapper(setter), |
| attrs, 0); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineElement(JSContext* cx, HandleObject obj, uint32_t index, HandleValue value, |
| unsigned attrs, Native getter, Native setter) |
| { |
| return DefineElement(cx, obj, index, value, attrs, getter, setter); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineElement(JSContext* cx, HandleObject obj, uint32_t index, HandleObject valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| RootedValue value(cx, ObjectValue(*valueArg)); |
| return DefineElement(cx, obj, index, value, attrs, getter, setter); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineElement(JSContext* cx, HandleObject obj, uint32_t index, HandleString valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| RootedValue value(cx, StringValue(valueArg)); |
| return DefineElement(cx, obj, index, value, attrs, getter, setter); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineElement(JSContext* cx, HandleObject obj, uint32_t index, int32_t valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| Value value = Int32Value(valueArg); |
| return DefineElement(cx, obj, index, HandleValue::fromMarkedLocation(&value), |
| attrs, getter, setter); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineElement(JSContext* cx, HandleObject obj, uint32_t index, uint32_t valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| Value value = NumberValue(valueArg); |
| return DefineElement(cx, obj, index, HandleValue::fromMarkedLocation(&value), |
| attrs, getter, setter); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DefineElement(JSContext* cx, HandleObject obj, uint32_t index, double valueArg, |
| unsigned attrs, Native getter, Native setter) |
| { |
| Value value = NumberValue(valueArg); |
| return DefineElement(cx, obj, index, HandleValue::fromMarkedLocation(&value), |
| attrs, getter, setter); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasPropertyById(JSContext* cx, HandleObject obj, HandleId id, bool* foundp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| |
| return HasProperty(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasProperty(JSContext* cx, HandleObject obj, const char* name, bool* foundp) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_HasPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, bool* foundp) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_HasPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasElement(JSContext* cx, HandleObject obj, uint32_t index, bool* foundp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| RootedId id(cx); |
| if (!IndexToId(cx, index, &id)) |
| return false; |
| return JS_HasPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasOwnPropertyById(JSContext* cx, HandleObject obj, HandleId id, bool* foundp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id); |
| |
| return HasOwnProperty(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_HasOwnProperty(JSContext* cx, HandleObject obj, const char* name, bool* foundp) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_HasOwnPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_ForwardGetPropertyTo(JSContext* cx, HandleObject obj, HandleId id, HandleValue receiver, |
| MutableHandleValue vp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id, receiver); |
| |
| return GetProperty(cx, obj, receiver, id, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_ForwardGetElementTo(JSContext* cx, HandleObject obj, uint32_t index, HandleObject receiver, |
| MutableHandleValue vp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| |
| return GetElement(cx, obj, receiver, index, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetPropertyById(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue vp) |
| { |
| RootedValue receiver(cx, ObjectValue(*obj)); |
| return JS_ForwardGetPropertyTo(cx, obj, id, receiver, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetProperty(JSContext* cx, HandleObject obj, const char* name, MutableHandleValue vp) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_GetPropertyById(cx, obj, id, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| MutableHandleValue vp) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_GetPropertyById(cx, obj, id, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_GetElement(JSContext* cx, HandleObject objArg, uint32_t index, MutableHandleValue vp) |
| { |
| return JS_ForwardGetElementTo(cx, objArg, index, objArg, vp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_ForwardSetPropertyTo(JSContext* cx, HandleObject obj, HandleId id, HandleValue v, |
| HandleValue receiver, ObjectOpResult& result) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id, receiver); |
| |
| return SetProperty(cx, obj, id, v, receiver, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetPropertyById(JSContext* cx, HandleObject obj, HandleId id, HandleValue v) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id); |
| |
| RootedValue receiver(cx, ObjectValue(*obj)); |
| ObjectOpResult ignored; |
| return SetProperty(cx, obj, id, v, receiver, ignored); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetProperty(JSContext* cx, HandleObject obj, const char* name, HandleValue v) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_SetPropertyById(cx, obj, id, v); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| HandleValue v) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_SetPropertyById(cx, obj, id, v); |
| } |
| |
| static bool |
| SetElement(JSContext* cx, HandleObject obj, uint32_t index, HandleValue v) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, v); |
| |
| RootedValue receiver(cx, ObjectValue(*obj)); |
| ObjectOpResult ignored; |
| return SetElement(cx, obj, index, v, receiver, ignored); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetElement(JSContext* cx, HandleObject obj, uint32_t index, HandleValue v) |
| { |
| return SetElement(cx, obj, index, v); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetElement(JSContext* cx, HandleObject obj, uint32_t index, HandleObject v) |
| { |
| RootedValue value(cx, ObjectOrNullValue(v)); |
| return SetElement(cx, obj, index, value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetElement(JSContext* cx, HandleObject obj, uint32_t index, HandleString v) |
| { |
| RootedValue value(cx, StringValue(v)); |
| return SetElement(cx, obj, index, value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetElement(JSContext* cx, HandleObject obj, uint32_t index, int32_t v) |
| { |
| RootedValue value(cx, NumberValue(v)); |
| return SetElement(cx, obj, index, value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetElement(JSContext* cx, HandleObject obj, uint32_t index, uint32_t v) |
| { |
| RootedValue value(cx, NumberValue(v)); |
| return SetElement(cx, obj, index, value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_SetElement(JSContext* cx, HandleObject obj, uint32_t index, double v) |
| { |
| RootedValue value(cx, NumberValue(v)); |
| return SetElement(cx, obj, index, value); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeletePropertyById(JSContext* cx, HandleObject obj, HandleId id, ObjectOpResult& result) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id); |
| |
| return DeleteProperty(cx, obj, id, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeleteProperty(JSContext* cx, HandleObject obj, const char* name, ObjectOpResult& result) |
| { |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return DeleteProperty(cx, obj, id, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeleteUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| ObjectOpResult& result) |
| { |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return DeleteProperty(cx, obj, id, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeleteElement(JSContext* cx, HandleObject obj, uint32_t index, ObjectOpResult& result) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| |
| return DeleteElement(cx, obj, index, result); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeletePropertyById(JSContext* cx, HandleObject obj, HandleId id) |
| { |
| ObjectOpResult ignored; |
| return JS_DeletePropertyById(cx, obj, id, ignored); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeleteProperty(JSContext* cx, HandleObject obj, const char* name) |
| { |
| ObjectOpResult ignored; |
| return JS_DeleteProperty(cx, obj, name, ignored); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeleteElement(JSContext* cx, HandleObject obj, uint32_t index) |
| { |
| ObjectOpResult ignored; |
| return JS_DeleteElement(cx, obj, index, ignored); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_Enumerate(JSContext* cx, HandleObject obj, JS::MutableHandle<IdVector> props) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| MOZ_ASSERT(props.empty()); |
| |
| AutoIdVector ids(cx); |
| if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY, &ids)) |
| return false; |
| |
| return props.append(ids.begin(), ids.end()); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::IsCallable(JSObject* obj) |
| { |
| return obj->isCallable(); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::IsConstructor(JSObject* obj) |
| { |
| return obj->isConstructor(); |
| } |
| |
| struct AutoLastFrameCheck |
| { |
| explicit AutoLastFrameCheck(JSContext* cx |
| MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
| : cx(cx) |
| { |
| MOZ_ASSERT(cx); |
| MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
| } |
| |
| ~AutoLastFrameCheck() { |
| if (cx->isExceptionPending() && |
| !JS_IsRunning(cx) && |
| (!cx->options().dontReportUncaught() && !cx->options().autoJSAPIOwnsErrorReporting())) { |
| ReportUncaughtException(cx); |
| } |
| } |
| |
| private: |
| JSContext* cx; |
| MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
| }; |
| |
| JS_PUBLIC_API(bool) |
| JS_CallFunctionValue(JSContext* cx, HandleObject obj, HandleValue fval, const HandleValueArray& args, |
| MutableHandleValue rval) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, fval, args); |
| AutoLastFrameCheck lfc(cx); |
| |
| return Invoke(cx, ObjectOrNullValue(obj), fval, args.length(), args.begin(), rval); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_CallFunction(JSContext* cx, HandleObject obj, HandleFunction fun, const HandleValueArray& args, |
| MutableHandleValue rval) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, fun, args); |
| AutoLastFrameCheck lfc(cx); |
| |
| return Invoke(cx, ObjectOrNullValue(obj), ObjectValue(*fun), args.length(), args.begin(), rval); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_CallFunctionName(JSContext* cx, HandleObject obj, const char* name, const HandleValueArray& args, |
| MutableHandleValue rval) |
| { |
| MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment())); |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, args); |
| AutoLastFrameCheck lfc(cx); |
| |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| |
| RootedValue v(cx); |
| RootedId id(cx, AtomToId(atom)); |
| if (!GetProperty(cx, obj, obj, id, &v)) |
| return false; |
| |
| return Invoke(cx, ObjectOrNullValue(obj), v, args.length(), args.begin(), rval); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::Call(JSContext* cx, HandleValue thisv, HandleValue fval, const JS::HandleValueArray& args, |
| MutableHandleValue rval) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, thisv, fval, args); |
| AutoLastFrameCheck lfc(cx); |
| |
| return Invoke(cx, thisv, fval, args.length(), args.begin(), rval); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::Construct(JSContext* cx, HandleValue fval, HandleObject newTarget, const JS::HandleValueArray& args, |
| MutableHandleValue rval) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, fval, newTarget, args); |
| AutoLastFrameCheck lfc(cx); |
| |
| if (!IsConstructor(fval)) { |
| ReportValueError(cx, JSMSG_NOT_CONSTRUCTOR, JSDVG_IGNORE_STACK, fval, nullptr); |
| return false; |
| } |
| |
| RootedValue newTargetVal(cx, ObjectValue(*newTarget)); |
| if (!IsConstructor(newTargetVal)) { |
| ReportValueError(cx, JSMSG_NOT_CONSTRUCTOR, JSDVG_IGNORE_STACK, newTargetVal, nullptr); |
| return false; |
| } |
| |
| ConstructArgs cargs(cx); |
| if (!FillArgumentsFromArraylike(cx, cargs, args)) |
| return false; |
| |
| return js::Construct(cx, fval, cargs, newTargetVal, rval); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS::Construct(JSContext* cx, HandleValue fval, const JS::HandleValueArray& args, |
| MutableHandleValue rval) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, fval, args); |
| AutoLastFrameCheck lfc(cx); |
| |
| if (!IsConstructor(fval)) { |
| ReportValueError(cx, JSMSG_NOT_CONSTRUCTOR, JSDVG_IGNORE_STACK, fval, nullptr); |
| return false; |
| } |
| |
| ConstructArgs cargs(cx); |
| if (!FillArgumentsFromArraylike(cx, cargs, args)) |
| return false; |
| |
| return js::Construct(cx, fval, cargs, fval, rval); |
| } |
| |
| |
| /* * */ |
| |
| JS_PUBLIC_API(bool) |
| JS_AlreadyHasOwnPropertyById(JSContext* cx, HandleObject obj, HandleId id, bool* foundp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj, id); |
| |
| if (!obj->isNative()) |
| return js::HasOwnProperty(cx, obj, id, foundp); |
| |
| RootedNativeObject nativeObj(cx, &obj->as<NativeObject>()); |
| RootedShape prop(cx); |
| NativeLookupOwnPropertyNoResolve(cx, nativeObj, id, &prop); |
| *foundp = !!prop; |
| return true; |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_AlreadyHasOwnProperty(JSContext* cx, HandleObject obj, const char* name, bool* foundp) |
| { |
| JSAtom* atom = Atomize(cx, name, strlen(name)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_AlreadyHasOwnPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_AlreadyHasOwnUCProperty(JSContext* cx, HandleObject obj, const char16_t* name, size_t namelen, |
| bool* foundp) |
| { |
| JSAtom* atom = AtomizeChars(cx, name, AUTO_NAMELEN(name, namelen)); |
| if (!atom) |
| return false; |
| RootedId id(cx, AtomToId(atom)); |
| return JS_AlreadyHasOwnPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_AlreadyHasOwnElement(JSContext* cx, HandleObject obj, uint32_t index, bool* foundp) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| RootedId id(cx); |
| if (!IndexToId(cx, index, &id)) |
| return false; |
| return JS_AlreadyHasOwnPropertyById(cx, obj, id, foundp); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_FreezeObject(JSContext* cx, HandleObject obj) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| return FreezeObject(cx, obj); |
| } |
| |
| JS_PUBLIC_API(bool) |
| JS_DeepFreezeObject(JSContext* cx, HandleObject obj) |
| { |
| AssertHeapIsIdle(cx); |
| CHECK_REQUEST(cx); |
| assertSameCompartment(cx, obj); |
| |
| /* Assume that non-extensible objects are already deep-frozen, to avoid divergence. */ |
| bool extensible; |
| if (!IsExtensible(cx, obj, &extensible)) |
| return false; |
| if (!extensible) |
| return true; |
| |
| if (!FreezeObject(cx, obj)) |
| return false; |
| |
| /* Walk slots in obj and if any value is a non-null object, seal it. */ |
| if (obj->isNative()) { |
| for (uint32_t i = 0, n = obj->as<NativeObject>().slotSpan(); i < n; ++i) { |
| const Value& v = obj->as<NativeObject>().getSlot(i); |
| if (v.isPrimitive()) |
| continue; |
| RootedObject obj(cx, &v.toObject()); |
|