1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/base/json_parser.h"
17
18 namespace panda::ecmascript::base {
InternalizeJsonProperty(JSThread * thread,const JSHandle<JSObject> & holder,const JSHandle<JSTaggedValue> & name,const JSHandle<JSTaggedValue> & receiver)19 JSHandle<JSTaggedValue> Internalize::InternalizeJsonProperty(JSThread *thread, const JSHandle<JSObject> &holder,
20 const JSHandle<JSTaggedValue> &name,
21 const JSHandle<JSTaggedValue> &receiver)
22 {
23 JSHandle<JSTaggedValue> objHandle(holder);
24 JSHandle<JSTaggedValue> val = JSTaggedValue::GetProperty(thread, objHandle, name).GetValue();
25 JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
26 if (val->IsECMAObject()) {
27 JSHandle<JSObject> obj = JSTaggedValue::ToObject(thread, val);
28 bool isArray = val->IsArray(thread);
29 if (isArray) {
30 JSHandle<JSTaggedValue> lenResult = JSTaggedValue::GetProperty(thread, val, lengthKey).GetValue();
31 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
32 JSTaggedNumber lenNumber = JSTaggedValue::ToLength(thread, lenResult);
33 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
34 uint32_t length = lenNumber.ToUint32();
35 JSMutableHandle<JSTaggedValue> keyUnknow(thread, JSTaggedValue::Undefined());
36 JSMutableHandle<JSTaggedValue> keyName(thread, JSTaggedValue::Undefined());
37 for (uint32_t i = 0; i < length; i++) {
38 // Let prop be ! ToString((I)).
39 keyUnknow.Update(JSTaggedValue(i));
40 keyName.Update(JSTaggedValue::ToString(thread, keyUnknow).GetTaggedValue());
41 RecurseAndApply(thread, obj, keyName, receiver);
42 }
43 } else {
44 // Let keys be ? EnumerableOwnPropertyNames(val, key).
45 JSHandle<TaggedArray> ownerNames(JSObject::EnumerableOwnNames(thread, obj));
46 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
47 uint32_t namesLength = ownerNames->GetLength();
48 JSMutableHandle<JSTaggedValue> keyName(thread, JSTaggedValue::Undefined());
49 for (uint32_t i = 0; i < namesLength; i++) {
50 keyName.Update(JSTaggedValue::GetProperty(thread, JSHandle<JSTaggedValue>(ownerNames), i)
51 .GetValue().GetTaggedValue());
52 RecurseAndApply(thread, obj, keyName, receiver);
53 }
54 }
55 }
56
57 // Return ? Call(receiver, holder, « name, val »).
58 const int32_t argsLength = 2; // 2: « name, val »
59 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
60 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, receiver, objHandle, undefined, argsLength);
61 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
62 info->SetCallArg(name.GetTaggedValue(), val.GetTaggedValue());
63 JSTaggedValue result = JSFunction::Call(info);
64 return JSHandle<JSTaggedValue>(thread, result);
65 }
66
RecurseAndApply(JSThread * thread,const JSHandle<JSObject> & holder,const JSHandle<JSTaggedValue> & name,const JSHandle<JSTaggedValue> & receiver)67 bool Internalize::RecurseAndApply(JSThread *thread, const JSHandle<JSObject> &holder,
68 const JSHandle<JSTaggedValue> &name, const JSHandle<JSTaggedValue> &receiver)
69 {
70 JSHandle<JSTaggedValue> value = InternalizeJsonProperty(thread, holder, name, receiver);
71 bool changeResult = false;
72
73 // If newElement is undefined, then Perform ? val.[[Delete]](P).
74 if (value->IsUndefined()) {
75 changeResult = JSObject::DeleteProperty(thread, holder, name);
76 } else {
77 // Perform ? CreateDataProperty(val, P, newElement)
78 changeResult = JSObject::CreateDataProperty(thread, holder, name, value);
79 }
80 return changeResult;
81 }
82 } // namespace panda::ecmascript::base
83