1 /* 2 * Copyright (c) 2020-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 #ifndef OHOS_ACELITE_JS_H 17 #define OHOS_ACELITE_JS_H 18 19 #include <stdlib.h> 20 #include "jerryscript.h" 21 #include "memory_heap.h" 22 23 namespace OHOS { 24 namespace ACELite { 25 typedef jerry_value_t JSValue; 26 typedef jerry_size_t JSSize; 27 typedef JSValue (*JSHandler)(const JSValue func, const JSValue ctx, const JSValue args[], JSSize argsSize); 28 29 void JSRelease(JSValue value); 30 31 class JSString final : public MemoryHeap { 32 public: 33 static JSValue Create(const char * const value); 34 static bool Is(JSValue target); 35 static char *Value(JSValue value); 36 }; 37 38 class JSFunction final : public MemoryHeap { 39 public: 40 static JSValue Create(JSHandler handler); 41 static JSValue Call(JSValue func, JSValue context, JSValue args[], JSSize size); 42 static bool Is(JSValue target); 43 }; 44 45 class JSObject final : public MemoryHeap { 46 public: 47 static JSValue Create(); 48 static JSValue Keys(JSValue target); 49 static JSValue Get(JSValue target, JSValue key); 50 static JSValue Get(JSValue target, const char * const prop); 51 static char *GetString(JSValue target, const char * const prop); 52 static double GetNumber(JSValue target, const char * const prop); 53 static bool GetBoolean(JSValue target, const char * const prop); 54 static void Set(JSValue target, const char * const prop, JSValue value); 55 static void SetString(JSValue target, const char * const prop, const char * const value); 56 static void SetNumber(JSValue target, const char * const prop, const double value); 57 static void SetBoolean(JSValue target, const char * const prop, const bool value); 58 static void Set(JSValue target, const char * const prop, JSHandler handler); 59 static bool Del(JSValue target, const char * const prop); 60 static JSValue Call(JSValue target, const char * const prop, JSValue args[], JSSize size); 61 static JSValue Call(JSValue target, const char * const prop); 62 static bool GetNativePointer(JSValue target, void **nativePointer); 63 static void SetNativePointer(JSValue target, void *nativePointer); 64 static bool DelNativePointer(JSValue target); 65 static bool Is(JSValue target); 66 static bool Has(JSValue target, const char *name); 67 }; 68 69 class JSGlobal final : public MemoryHeap { 70 public: 71 static JSValue Get(); 72 static JSValue Get(const char * const prop); 73 static void Set(const char * const prop, JSValue value); 74 static void Del(const char * const props); 75 static JSValue Call(const char * const prop, JSValue args[], JSSize size); 76 static JSValue Call(const char * const prop); 77 }; 78 79 class JSUndefined final : public MemoryHeap { 80 public: 81 static JSValue Create(); 82 static bool Is(JSValue value); 83 }; 84 85 class JSNumber final : public MemoryHeap { 86 public: 87 static JSValue Create(double number); 88 static bool Is(JSValue target); 89 static double Value(JSValue value); 90 }; 91 92 class JSBoolean final : public MemoryHeap { 93 public: 94 static JSValue Create(bool value); 95 static bool Is(JSValue target); 96 static bool Value(JSValue value); 97 }; 98 99 class JSArray final : public MemoryHeap { 100 public: 101 static JSValue Create(uint32_t size); 102 static uint32_t Length(const JSValue array); 103 static JSValue Get(JSValue array, uint32_t index); 104 static char *GetString(JSValue array, uint32_t index); 105 static JSValue Map(JSValue array, JSValue func); 106 static void Push(JSValue array, JSValue element); 107 static bool Is(JSValue target); 108 }; 109 class JSError final : public MemoryHeap { 110 public: 111 static bool Is(JSValue target); 112 }; 113 } // namespace ACELite 114 } // namespace OHOS 115 #endif // OHOS_ACELITE_JS_H 116