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