• 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/ecma_string-inl.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/mem/assert_scope.h"
22 #include "ecmascript/object_factory.h"
23 
24 namespace panda::ecmascript {
25 // ES6 9.4.3.4 StringCreate( value, prototype)
StringCreate(JSThread * thread,const JSHandle<JSTaggedValue> & value)26 JSHandle<JSPrimitiveRef> JSPrimitiveRef::StringCreate(JSThread *thread, const JSHandle<JSTaggedValue> &value)
27 {
28     // 1. ReturnIfAbrupt(prototype).
29     // 2. Assert: Type(value) is String.
30     ASSERT(value->IsString());
31     // 3. Let S be a newly created String exotic object.
32     // 4. Set the [[StringData]] internal slot of S to value.
33     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
34     JSHandle<JSTaggedValue> str(factory->NewJSString(value));
35     // 11. Let length be the number of code unit elements in value.
36     // 12. Let status be DefinePropertyOrThrow(S, "length", PropertyDescriptor{[[Value]]: length, [[Writable]]:
37     // false, [[Enumerable]]: false, [[Configurable]]: false }).
38     JSHandle<JSTaggedValue> lengthStr = thread->GlobalConstants()->GetHandledLengthString();
39 
40     uint32_t length = EcmaStringAccessor(value->GetTaggedObject()).GetLength();
41     PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(length)), false, false, false);
42     [[maybe_unused]] bool status = JSTaggedValue::DefinePropertyOrThrow(thread, str, lengthStr, desc);
43     // 13. Assert: status is not an abrupt completion.
44     // 14. Return S.
45     ASSERT(status);
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;
53     {
54         DISALLOW_GARBAGE_COLLECTION;
55         EcmaString *str = EcmaString::Cast(JSPrimitiveRef::Cast(*obj)->GetValue().GetTaggedObject());
56         if (EcmaStringAccessor(str).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(str).Get(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