• 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 "jerryscript_native_array.h"
17 
JerryScriptNativeArray(JerryScriptNativeEngine * engine,jerry_value_t value)18 JerryScriptNativeArray::JerryScriptNativeArray(JerryScriptNativeEngine* engine, jerry_value_t value)
19     : JerryScriptNativeObject(engine, value)
20 {
21 }
22 
JerryScriptNativeArray(JerryScriptNativeEngine * engine,int length)23 JerryScriptNativeArray::JerryScriptNativeArray(JerryScriptNativeEngine* engine, int length)
24     : JerryScriptNativeArray(engine, jerry_create_array(length))
25 {
26 }
27 
~JerryScriptNativeArray()28 JerryScriptNativeArray::~JerryScriptNativeArray() {}
29 
GetInterface(int interfaceId)30 void* JerryScriptNativeArray::GetInterface(int interfaceId)
31 {
32     return (NativeArray::INTERFACE_ID == interfaceId) ? (NativeArray*)this
33                                                       : JerryScriptNativeObject::GetInterface(interfaceId);
34 }
35 
SetElement(uint32_t index,NativeValue * value)36 bool JerryScriptNativeArray::SetElement(uint32_t index, NativeValue* value)
37 {
38     jerry_value_t returnValue = jerry_set_property_by_index(value_, index, value_);
39     jerry_release_value(returnValue);
40     return true;
41 }
42 
GetElement(uint32_t index)43 NativeValue* JerryScriptNativeArray::GetElement(uint32_t index)
44 {
45     jerry_value_t returnValue = jerry_get_property_by_index(value_, index);
46     return JerryScriptNativeEngine::JerryValueToNativeValue(engine_, returnValue);
47 }
48 
HasElement(uint32_t index)49 bool JerryScriptNativeArray::HasElement(uint32_t index)
50 {
51     jerry_value_t returnValue = jerry_get_property_by_index(value_, index);
52     bool result = !jerry_value_is_undefined(returnValue);
53     jerry_release_value(returnValue);
54     return result;
55 }
56 
DeleteElement(uint32_t index)57 bool JerryScriptNativeArray::DeleteElement(uint32_t index)
58 {
59     return jerry_delete_property_by_index(value_, index);
60 }
61 
GetLength()62 uint32_t JerryScriptNativeArray::GetLength()
63 {
64     return jerry_get_array_length(value_);
65 }
66