• 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/js_primitive_ref.h"
17 
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/ecma_string-inl.h"
20 
21 namespace panda::ecmascript {
22 // ES6 9.4.3.4 StringCreate( value, prototype)
StringCreate(JSThread * thread,const JSHandle<JSTaggedValue> & value,const JSHandle<JSTaggedValue> & newTarget)23 JSHandle<JSPrimitiveRef> JSPrimitiveRef::StringCreate(JSThread *thread, const JSHandle<JSTaggedValue> &value,
24                                                       const JSHandle<JSTaggedValue> &newTarget)
25 {
26     ASSERT(value->IsString());
27     // 1. Let S be MakeBasicObject(<<[[Prototype]], [[Extensible]], [[StringData]]>>).
28     // 2. Set S.[[Prototype]] to prototype.
29     // 3. Set S.[[StringData]] to value.
30     // 4. Set S.[[GetOwnProperty]] as specified.
31     // 5. Set S.[[DefineOwnProperty]] as specified.
32     // 6. Set S.[[OwnPropertyKeys]] as specified.
33     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
34     JSHandle<JSTaggedValue> str(factory->NewJSString(value, newTarget));
35     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSPrimitiveRef, thread);
36     // 7. Let length be the number of code unit elements in value.
37     JSHandle<JSTaggedValue> lengthStr = thread->GlobalConstants()->GetHandledLengthString();
38     uint32_t length = EcmaStringAccessor(value->GetTaggedObject()).GetLength();
39     // 8. Perform ! DefinePropertyOrThrow(S, "length", PropertyDescriptor { [[Value]]: F(length),
40     //    [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
41     PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(length)), false, false, false);
42     [[maybe_unused]] bool status = JSTaggedValue::DefinePropertyOrThrow(thread, str, lengthStr, desc);
43     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSPrimitiveRef, thread);
44     ASSERT(status);
45     // 9. Return S.
46     return JSHandle<JSPrimitiveRef>(str);
47 }
48 
StringGetIndexProperty(const JSThread * thread,const JSHandle<JSObject> & obj,uint32_t index,PropertyDescriptor * desc)49 bool JSPrimitiveRef::StringGetIndexProperty(const JSThread *thread, const JSHandle<JSObject> &obj, uint32_t index,
50                                             PropertyDescriptor *desc)
51 {
52     uint16_t tmpChar = 0;
53     {
54         JSHandle<EcmaString> strHandle(thread, EcmaString::Cast(JSPrimitiveRef::Cast(*obj)->GetValue(thread)));
55         JSHandle<EcmaString> strFlat(thread, EcmaStringAccessor::Flatten(thread->GetEcmaVM(), strHandle));
56         if (EcmaStringAccessor(strFlat).GetLength() <= index) {
57             return false;
58         }
59         // 10. Let resultStr be a String value of length 1, containing one code unit from str, specifically the code
60         // unit at index index
61         tmpChar = EcmaStringAccessor(strFlat).Get(thread, index);
62     }
63     JSHandle<JSTaggedValue> value(thread->GetEcmaVM()->GetFactory()->NewFromUtf16(&tmpChar, 1));
64     // 11. Return a PropertyDescriptor{ [[Value]]: resultStr, [[Enumerable]]: true, [[Writable]]: false,
65     // [[Configurable]]: false }.
66     desc->SetValue(value);
67     desc->SetEnumerable(true);
68     desc->SetWritable(false);
69     desc->SetConfigurable(false);
70     return true;
71 }
72 // ES6 9.4.3.3 [[OwnPropertyKeys]] ( )
73 }  // namespace panda::ecmascript
74