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 "js_primitive_ref.h"
17 #include "ecmascript/ecma_string-inl.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/mem/assert_scope-inl.h"
21 #include "ecmascript/object_factory.h"
22
23 namespace panda::ecmascript {
24 // ES6 9.4.3.4 StringCreate( value, prototype)
StringCreate(JSThread * thread,const JSHandle<JSTaggedValue> & value)25 JSHandle<JSPrimitiveRef> JSPrimitiveRef::StringCreate(JSThread *thread, const JSHandle<JSTaggedValue> &value)
26 {
27 // 1. ReturnIfAbrupt(prototype).
28 // 2. Assert: Type(value) is String.
29 ASSERT(value->IsString());
30 // 3. Let S be a newly created String exotic object.
31 // 4. Set the [[StringData]] internal slot of S to value.
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 JSHandle<JSTaggedValue> str(factory->NewJSString(value));
34 // 11. Let length be the number of code unit elements in value.
35 // 12. Let status be DefinePropertyOrThrow(S, "length", PropertyDescriptor{[[Value]]: length, [[Writable]]:
36 // false, [[Enumerable]]: false, [[Configurable]]: false }).
37 JSHandle<JSTaggedValue> lengthStr = thread->GlobalConstants()->GetHandledLengthString();
38
39 int32_t length = EcmaString::Cast(value->GetTaggedObject())->GetLength();
40 PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(length)), false, false, false);
41 [[maybe_unused]] bool status = JSTaggedValue::DefinePropertyOrThrow(thread, str, lengthStr, desc);
42 // 13. Assert: status is not an abrupt completion.
43 // 14. Return S.
44 ASSERT(status);
45 return JSHandle<JSPrimitiveRef>(str);
46 }
47
StringGetIndexProperty(const JSThread * thread,const JSHandle<JSObject> & obj,uint32_t index,PropertyDescriptor * desc)48 bool JSPrimitiveRef::StringGetIndexProperty(const JSThread *thread, const JSHandle<JSObject> &obj, uint32_t index,
49 PropertyDescriptor *desc)
50 {
51 uint16_t tmpChar;
52 {
53 DISALLOW_GARBAGE_COLLECTION;
54 EcmaString *str = EcmaString::Cast(JSPrimitiveRef::Cast(*obj)->GetValue().GetTaggedObject());
55 if (str->GetLength() <= index) {
56 return false;
57 }
58 // 10. Let resultStr be a String value of length 1, containing one code unit from str, specifically the code
59 // unit at index index
60 tmpChar = str->At(index);
61 }
62 JSHandle<JSTaggedValue> value(thread->GetEcmaVM()->GetFactory()->NewFromUtf16(&tmpChar, 1));
63 // 11. Return a PropertyDescriptor{ [[Value]]: resultStr, [[Enumerable]]: true, [[Writable]]: false,
64 // [[Configurable]]: false }.
65 desc->SetValue(value);
66 desc->SetEnumerable(true);
67 desc->SetWritable(false);
68 desc->SetConfigurable(false);
69 return true;
70 }
71 // ES6 9.4.3.3 [[OwnPropertyKeys]] ( )
72 } // namespace panda::ecmascript
73