arkcompiler_ets_runtime/ecmascript/builtins/builtins_weak_map.cpp
yaoyuan b80c0196ca Description:
Reduce redundant code by using predefined global constants instead of creating new constant strings
Move all global constant strings into nonmovable space to improve code efficiency.

Issues: https://gitee.com/openharmony/ark_js_runtime/issues/I588TY

Signed-off-by: yaoyuan <yuanyao14@huawei.com>
Change-Id: I1f92188175c3129c5d8cec72a0396f11ce17c1c7
2022-05-31 17:38:54 +08:00

166 lines
7.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/builtins/builtins_weak_map.h"
#include "ecmascript/builtins/builtins_map.h"
#include "ecmascript/ecma_vm.h"
#include "ecmascript/global_env.h"
#include "ecmascript/interpreter/interpreter.h"
#include "ecmascript/js_map_iterator.h"
#include "ecmascript/js_weak_container.h"
#include "ecmascript/linked_hash_table.h"
#include "ecmascript/object_factory.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsWeakMap::WeakMapConstructor(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Constructor);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
// 1.If NewTarget is undefined, throw a TypeError exception
JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
if (newTarget->IsUndefined()) {
// throw type error
THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
}
// 2.Let WeakMap be OrdinaryCreateFromConstructor(NewTarget, "%WeakMapPrototype%", «‍[[WeakMapData]]» ).
JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
// 3.returnIfAbrupt()
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSWeakMap> weakMap = JSHandle<JSWeakMap>::Cast(obj);
// 4.Set weakmaps [[WeakMapData]] internal slot to a new empty List.
JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
weakMap->SetLinkedMap(thread, linkedMap);
// add data into set from iterable
// 5.If iterable is not present, let iterable be undefined.
// 6.If iterable is either undefined or null, let iter be undefined.
JSHandle<JSTaggedValue> iterable = GetCallArg(argv, 0);
// 8.If iter is undefined, return set
if (iterable->IsUndefined() || iterable->IsNull()) {
return weakMap.GetTaggedValue();
}
if (!iterable->IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "iterable is not object", JSTaggedValue::Exception());
}
// Let adder be Get(weakMap, "set").
JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledSetString();
JSHandle<JSTaggedValue> adder =
JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(weakMap), adderKey).GetValue();
// ReturnIfAbrupt(adder).
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
// If IsCallable(adder) is false, throw a TypeError exception
if (!adder->IsCallable()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
}
return BuiltinsMap::AddEntriesFromIterable(thread, obj, iterable, adder, factory);
}
JSTaggedValue BuiltinsWeakMap::Delete(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Delete);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> self = GetThis(argv);
// 2.If Type(S) is not Object, throw a TypeError exception.
// 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!self->IsJSWeakMap()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
}
JSHandle<JSWeakMap> weakMap(self);
JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
// 5.if Type(key) is not Object, return false.
if (!key->IsHeapObject()) {
return GetTaggedBoolean(false);
}
return GetTaggedBoolean(JSWeakMap::Delete(thread, weakMap, key));
}
JSTaggedValue BuiltinsWeakMap::Has(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Has);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> self(GetThis(argv));
// 2.If Type(S) is not Object, throw a TypeError exception.
// 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!self->IsJSWeakMap()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
}
JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
// 5.if Type(key) is not Object, return false.
if (!key->IsHeapObject()) {
return GetTaggedBoolean(false);
}
return GetTaggedBoolean(jsWeakMap->Has(key.GetTaggedValue()));
}
JSTaggedValue BuiltinsWeakMap::Get(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Get);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> self(GetThis(argv));
// 2.If Type(S) is not Object, throw a TypeError exception.
// 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!self->IsJSWeakMap()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
}
JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
if (!key->IsHeapObject()) {
return JSTaggedValue::Undefined();
}
return jsWeakMap->Get(key.GetTaggedValue());
}
JSTaggedValue BuiltinsWeakMap::Set(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Set);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> self = GetThis(argv);
// 2.If Type(S) is not Object, throw a TypeError exception.
// 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!self->IsJSWeakMap()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
if (!key->IsHeapObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not an object.", JSTaggedValue::Exception());
}
if (key->IsSymbol() || key->IsString()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "key is Symblol or String", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
JSHandle<JSWeakMap> map(self);
JSWeakMap::Set(thread, map, key, value);
return map.GetTaggedValue();
}
} // namespace panda::ecmascript::builtins