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
20 namespace panda::ecmascript {
21 // ES6 9.4.3.4 StringCreate( value, prototype)
StringCreate(JSThread * thread,const JSHandle<JSTaggedValue> & value,const JSHandle<JSTaggedValue> & newTarget)22 JSHandle<JSPrimitiveRef> JSPrimitiveRef::StringCreate(JSThread *thread, const JSHandle<JSTaggedValue> &value,
23 const JSHandle<JSTaggedValue> &newTarget)
24 {
25 ASSERT(value->IsString());
26 // 1. Let S be MakeBasicObject(<<[[Prototype]], [[Extensible]], [[StringData]]>>).
27 // 2. Set S.[[Prototype]] to prototype.
28 // 3. Set S.[[StringData]] to value.
29 // 4. Set S.[[GetOwnProperty]] as specified.
30 // 5. Set S.[[DefineOwnProperty]] as specified.
31 // 6. Set S.[[OwnPropertyKeys]] as specified.
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 JSHandle<JSTaggedValue> str(factory->NewJSString(value, newTarget));
34 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSPrimitiveRef, thread);
35 // 7. Let length be the number of code unit elements in value.
36 JSHandle<JSTaggedValue> lengthStr = thread->GlobalConstants()->GetHandledLengthString();
37 uint32_t length = EcmaStringAccessor(value->GetTaggedObject()).GetLength();
38 // 8. Perform ! DefinePropertyOrThrow(S, "length", PropertyDescriptor { [[Value]]: F(length),
39 // [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
40 PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(length)), false, false, false);
41 [[maybe_unused]] bool status = JSTaggedValue::DefinePropertyOrThrow(thread, str, lengthStr, desc);
42 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSPrimitiveRef, thread);
43 ASSERT(status);
44 // 9. Return S.
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 = 0;
52 {
53 JSHandle<EcmaString> strHandle(thread, EcmaString::Cast(JSPrimitiveRef::Cast(*obj)->GetValue()));
54 JSHandle<EcmaString> strFlat(thread, EcmaStringAccessor::Flatten(thread->GetEcmaVM(), strHandle));
55 if (EcmaStringAccessor(strFlat).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 = EcmaStringAccessor(strFlat).Get(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