2017-08-12 16:48:01 +00:00
|
|
|
/*
|
2022-10-23 02:55:20 +00:00
|
|
|
* Copyright (C) 2006-2020 Apple Inc. All rights reserved.
|
2017-08-12 16:48:01 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "JSValueRef.h"
|
|
|
|
|
|
|
|
#include "APICast.h"
|
|
|
|
#include "APIUtils.h"
|
|
|
|
#include "DateInstance.h"
|
|
|
|
#include "JSAPIWrapperObject.h"
|
|
|
|
#include "JSCInlines.h"
|
|
|
|
#include "JSCallbackObject.h"
|
|
|
|
#include "JSONObject.h"
|
|
|
|
#include "LiteralParser.h"
|
|
|
|
#include "Protect.h"
|
|
|
|
#include <wtf/Assertions.h>
|
|
|
|
#include <wtf/text/WTFString.h>
|
|
|
|
|
|
|
|
#if PLATFORM(MAC)
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLE(REMOTE_INSPECTOR)
|
|
|
|
#include "JSGlobalObjectInspectorController.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace JSC;
|
|
|
|
|
|
|
|
::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return kJSTypeUndefined;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
|
|
|
#else
|
|
|
|
JSValue jsValue = toJS(value);
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
if (jsValue.isUndefined())
|
|
|
|
return kJSTypeUndefined;
|
2022-10-23 02:55:20 +00:00
|
|
|
if (!jsValue || jsValue.isNull())
|
2017-08-12 16:48:01 +00:00
|
|
|
return kJSTypeNull;
|
|
|
|
if (jsValue.isBoolean())
|
|
|
|
return kJSTypeBoolean;
|
|
|
|
if (jsValue.isNumber())
|
|
|
|
return kJSTypeNumber;
|
|
|
|
if (jsValue.isString())
|
|
|
|
return kJSTypeString;
|
2020-08-29 13:27:11 +00:00
|
|
|
if (jsValue.isSymbol())
|
|
|
|
return kJSTypeSymbol;
|
2017-08-12 16:48:01 +00:00
|
|
|
ASSERT(jsValue.isObject());
|
|
|
|
return kJSTypeObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isUndefined();
|
|
|
|
#else
|
|
|
|
return toJS(value).isUndefined();
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isNull();
|
|
|
|
#else
|
|
|
|
return !value || toJS(value).isNull();
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isBoolean();
|
|
|
|
#else
|
|
|
|
return toJS(value).isBoolean();
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isNumber();
|
|
|
|
#else
|
|
|
|
return toJS(value).isNumber();
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsString(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isString();
|
|
|
|
#else
|
|
|
|
return value && toJS(value).isString();
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isObject();
|
|
|
|
#else
|
|
|
|
return value && toJS(value).isObject();
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 13:27:11 +00:00
|
|
|
bool JSValueIsSymbol(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toJS(globalObject, value).isSymbol();
|
|
|
|
#else
|
|
|
|
return value && toJS(value).isSymbol();
|
|
|
|
#endif
|
2020-08-29 13:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 16:48:01 +00:00
|
|
|
bool JSValueIsArray(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
return toJS(globalObject, value).inherits<JSArray>(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsDate(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
return toJS(globalObject, value).inherits<DateInstance>(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
|
|
|
|
{
|
|
|
|
if (!ctx || !jsClass) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
if (JSObject* o = jsValue.getObject()) {
|
2020-08-29 13:27:11 +00:00
|
|
|
if (o->inherits<JSProxy>(vm))
|
2017-08-12 16:48:01 +00:00
|
|
|
o = jsCast<JSProxy*>(o)->target();
|
|
|
|
|
2020-08-29 13:27:11 +00:00
|
|
|
if (o->inherits<JSCallbackObject<JSGlobalObject>>(vm))
|
2017-08-12 16:48:01 +00:00
|
|
|
return jsCast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
|
2022-10-23 02:55:20 +00:00
|
|
|
if (o->inherits<JSCallbackObject<JSNonFinalObject>>(vm))
|
|
|
|
return jsCast<JSCallbackObject<JSNonFinalObject>*>(o)->inherits(jsClass);
|
2017-08-12 16:48:01 +00:00
|
|
|
#if JSC_OBJC_API_ENABLED
|
2020-08-29 13:27:11 +00:00
|
|
|
if (o->inherits<JSCallbackObject<JSAPIWrapperObject>>(vm))
|
2017-08-12 16:48:01 +00:00
|
|
|
return jsCast<JSCallbackObject<JSAPIWrapperObject>*>(o)->inherits(jsClass);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
2020-08-29 13:27:11 +00:00
|
|
|
JSLockHolder locker(vm);
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsA = toJS(globalObject, a);
|
|
|
|
JSValue jsB = toJS(globalObject, b);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
bool result = JSValue::equal(globalObject, jsA, jsB); // false if an exception is thrown
|
|
|
|
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
|
|
|
|
return false;
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsA = toJS(globalObject, a);
|
|
|
|
JSValue jsB = toJS(globalObject, b);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
return JSValue::strictEqual(globalObject, jsA, jsB);
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
2020-08-29 13:27:11 +00:00
|
|
|
JSLockHolder locker(vm);
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
JSObject* jsConstructor = toJS(constructor);
|
2020-08-29 13:27:11 +00:00
|
|
|
if (!jsConstructor->structure(vm)->typeInfo().implementsHasInstance())
|
2017-08-12 16:48:01 +00:00
|
|
|
return false;
|
2022-10-23 02:55:20 +00:00
|
|
|
bool result = jsConstructor->hasInstance(globalObject, jsValue); // false if an exception is thrown
|
|
|
|
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
|
|
|
|
return false;
|
2017-08-12 16:48:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSValueMakeUndefined(JSContextRef ctx)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toRef(globalObject, jsUndefined());
|
|
|
|
#else
|
|
|
|
return toRef(jsUndefined());
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSValueMakeNull(JSContextRef ctx)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toRef(globalObject, jsNull());
|
|
|
|
#else
|
|
|
|
return toRef(jsNull());
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toRef(globalObject, jsBoolean(value));
|
|
|
|
#else
|
|
|
|
return toRef(jsBoolean(value));
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
#if !CPU(ADDRESS64)
|
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
|
|
|
return toRef(globalObject, jsNumber(purifyNaN(value)));
|
|
|
|
#else
|
|
|
|
return toRef(jsNumber(purifyNaN(value)));
|
|
|
|
#endif
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 13:27:11 +00:00
|
|
|
JSValueRef JSValueMakeSymbol(JSContextRef ctx, JSStringRef description)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
|
|
|
JSLockHolder locker(globalObject);
|
2020-08-29 13:27:11 +00:00
|
|
|
|
|
|
|
if (!description)
|
2022-10-23 02:55:20 +00:00
|
|
|
return toRef(globalObject, Symbol::create(vm));
|
|
|
|
return toRef(globalObject, Symbol::createWithDescription(vm, description->string()));
|
2020-08-29 13:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 16:48:01 +00:00
|
|
|
JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
|
|
|
JSLockHolder locker(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
return toRef(globalObject, jsString(vm, string ? string->string() : String()));
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
String str = string->string();
|
|
|
|
unsigned length = str.length();
|
|
|
|
if (!length || str.is8Bit()) {
|
2022-10-23 02:55:20 +00:00
|
|
|
LiteralParser<LChar> parser(globalObject, str.characters8(), length, StrictJSON);
|
|
|
|
return toRef(globalObject, parser.tryLiteralParse());
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
LiteralParser<UChar> parser(globalObject, str.characters16(), length, StrictJSON);
|
|
|
|
return toRef(globalObject, parser.tryLiteralParse());
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
2020-08-29 13:27:11 +00:00
|
|
|
JSLockHolder locker(vm);
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue value = toJS(globalObject, apiValue);
|
|
|
|
String result = JSONStringify(globalObject, value, indent);
|
2017-08-12 16:48:01 +00:00
|
|
|
if (exception)
|
2022-10-23 02:55:20 +00:00
|
|
|
*exception = nullptr;
|
|
|
|
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
|
|
|
|
return nullptr;
|
2020-08-29 13:27:11 +00:00
|
|
|
return OpaqueJSString::tryCreate(result).leakRef();
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
|
|
|
return jsValue.toBoolean(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return PNaN;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
2020-08-29 13:27:11 +00:00
|
|
|
JSLockHolder locker(vm);
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
double number = jsValue.toNumber(globalObject);
|
|
|
|
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
|
2017-08-12 16:48:01 +00:00
|
|
|
number = PNaN;
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
2020-08-29 13:27:11 +00:00
|
|
|
JSLockHolder locker(vm);
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
auto stringRef(OpaqueJSString::tryCreate(jsValue.toWTFString(globalObject)));
|
|
|
|
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
|
2017-08-12 16:48:01 +00:00
|
|
|
stringRef = nullptr;
|
|
|
|
return stringRef.leakRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
2022-10-23 02:55:20 +00:00
|
|
|
return nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
VM& vm = globalObject->vm();
|
2020-08-29 13:27:11 +00:00
|
|
|
JSLockHolder locker(vm);
|
|
|
|
auto scope = DECLARE_CATCH_SCOPE(vm);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJS(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSObjectRef objectRef = toRef(jsValue.toObject(globalObject));
|
|
|
|
if (handleExceptionIfNeeded(scope, ctx, exception) == ExceptionStatus::DidThrow)
|
|
|
|
objectRef = nullptr;
|
2017-08-12 16:48:01 +00:00
|
|
|
return objectRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSValueProtect(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
|
|
|
if (!ctx) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return;
|
|
|
|
}
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJSForGC(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
gcProtect(jsValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
|
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
JSGlobalObject* globalObject = toJS(ctx);
|
|
|
|
JSLockHolder locker(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue jsValue = toJSForGC(globalObject, value);
|
2017-08-12 16:48:01 +00:00
|
|
|
gcUnprotect(jsValue);
|
|
|
|
}
|