• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
26     JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
27     if (val->IsECMAObject()) {
28         JSHandle<JSObject> obj = JSTaggedValue::ToObject(thread, val);
29         RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
30         bool isArray = val->IsArray(thread);
31         if (isArray) {
32             JSHandle<JSTaggedValue> lenResult = JSTaggedValue::GetProperty(thread, val, lengthKey).GetValue();
33             RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
34             JSTaggedNumber lenNumber = JSTaggedValue::ToLength(thread, lenResult);
35             RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
36             uint32_t length = lenNumber.ToUint32();
37             JSMutableHandle<JSTaggedValue> keyUnknow(thread, JSTaggedValue::Undefined());
38             JSMutableHandle<JSTaggedValue> keyName(thread, JSTaggedValue::Undefined());
39             for (uint32_t i = 0; i < length; i++) {
40                 // Let prop be ! ToString((I)).
41                 keyUnknow.Update(JSTaggedValue(i));
42                 keyName.Update(JSTaggedValue::ToString(thread, keyUnknow).GetTaggedValue());
43                 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
44                 RecurseAndApply(thread, obj, keyName, receiver);
45             }
46         } else {
47             // Let keys be ? EnumerableOwnPropertyNames(val, key).
48             JSHandle<TaggedArray> ownerNames(JSObject::EnumerableOwnNames(thread, obj));
49             RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
50             uint32_t namesLength = ownerNames->GetLength();
51             JSMutableHandle<JSTaggedValue> keyName(thread, JSTaggedValue::Undefined());
52             for (uint32_t i = 0; i < namesLength; i++) {
53                 keyName.Update(ownerNames->Get(i));
54                 RecurseAndApply(thread, obj, keyName, receiver);
55             }
56         }
57     }
58 
59     // Return ? Call(receiver, holder, « name, val »).
60     const uint32_t argsLength = 2;  // 2: « name, val »
61     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
62     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, receiver, objHandle, undefined, argsLength);
63     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
64     info->SetCallArg(name.GetTaggedValue(), val.GetTaggedValue());
65     JSTaggedValue result = JSFunction::Call(info);
66     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
67     return JSHandle<JSTaggedValue>(thread, result);
68 }
69 
RecurseAndApply(JSThread * thread,const JSHandle<JSObject> & holder,const JSHandle<JSTaggedValue> & name,const JSHandle<JSTaggedValue> & receiver)70 bool Internalize::RecurseAndApply(JSThread *thread, const JSHandle<JSObject> &holder,
71                                   const JSHandle<JSTaggedValue> &name, const JSHandle<JSTaggedValue> &receiver)
72 {
73     JSHandle<JSTaggedValue> value = InternalizeJsonProperty(thread, holder, name, receiver);
74     bool changeResult = false;
75 
76     // If newElement is undefined, then Perform ? val.[[Delete]](P).
77     if (value->IsUndefined()) {
78         changeResult = JSObject::DeleteProperty(thread, holder, name);
79     } else {
80         // Perform ? CreateDataProperty(val, P, newElement)
81         changeResult = JSObject::CreateDataProperty(thread, holder, name, value);
82     }
83     return changeResult;
84 }
85 }  // namespace panda::ecmascript::base
86