/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/builtins/builtins_json.h" #include "ecmascript/base/json_parser.h" #include "ecmascript/base/json_stringifier.h" #include "ecmascript/base/number_helper.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/global_env.h" #include "ecmascript/js_handle.h" #include "ecmascript/js_primitive_ref.h" #include "ecmascript/js_tagged_value-inl.h" #include "ecmascript/object_factory.h" namespace panda::ecmascript::builtins { // 24.5.1 JSTaggedValue BuiltinsJson::Parse(EcmaRuntimeCallInfo *argv) { BUILTINS_API_TRACE(argv->GetThread(), Json, Parse); ASSERT(argv); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); uint32_t argc = argv->GetArgsNumber(); if (argc == 0) { JSHandle syntaxError = factory->GetJSError(base::ErrorType::SYNTAX_ERROR, "arg is empty"); THROW_NEW_ERROR_AND_RETURN_VALUE(thread, syntaxError.GetTaggedValue(), JSTaggedValue::Exception()); } JSHandle msg = GetCallArg(argv, 0); JSHandle parseString = JSTaggedValue::ToString(thread, msg); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); JSHandle result; if (EcmaStringAccessor(parseString).IsUtf8()) { panda::ecmascript::base::JsonParser parser(thread); result = parser.ParseUtf8(*parseString); } else { panda::ecmascript::base::JsonParser parser(thread); result = parser.ParseUtf16(*parseString); } RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); JSTaggedValue reviver = JSTaggedValue::Undefined(); if (argc == 2) { // 2: 2 args reviver = GetCallArg(argv, 1).GetTaggedValue(); if (reviver.IsCallable()) { JSHandle callbackfnHandle(thread, reviver); // Let root be ! OrdinaryObjectCreate(%Object.prototype%). JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle constructor(env->GetObjectFunction()); JSHandle root = factory->NewJSObjectByConstructor(constructor); // Let rootName be the empty String. JSHandle rootName(factory->GetEmptyString()); // Perform ! CreateDataPropertyOrThrow(root, rootName, unfiltered). bool success = JSObject::CreateDataProperty(thread, root, rootName, result); if (success) { result = base::Internalize::InternalizeJsonProperty(thread, root, rootName, callbackfnHandle); } } } return result.GetTaggedValue(); } // 24.5.2 JSTaggedValue BuiltinsJson::Stringify(EcmaRuntimeCallInfo *argv) { BUILTINS_API_TRACE(argv->GetThread(), Json, Stringify); ASSERT(argv); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); uint32_t argc = argv->GetArgsNumber(); JSTaggedValue value = GetCallArg(argv, 0).GetTaggedValue(); JSTaggedValue replacer = JSTaggedValue::Undefined(); JSTaggedValue gap = JSTaggedValue::Undefined(); if (argc == 2) { // 2: 2 args replacer = GetCallArg(argv, 1).GetTaggedValue(); } else if (argc == 3) { // 3: 3 args replacer = GetCallArg(argv, 1).GetTaggedValue(); gap = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD).GetTaggedValue(); } JSHandle handleValue(thread, value); JSHandle handleReplacer(thread, replacer); JSHandle handleGap(thread, gap); panda::ecmascript::base::JsonStringifier stringifier(thread); JSHandle result = stringifier.Stringify(handleValue, handleReplacer, handleGap); return result.GetTaggedValue(); } } // namespace panda::ecmascript::builtins