1 /* 2 * Copyright (c) 2024 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 #ifndef JSVM_UTILS_H 16 #define JSVM_UTILS_H 17 18 #if defined(OHOS_JSVM_HAP) && !defined(OHOS_JSVM_XTS) 19 #include "ark_runtime/jsvm.h" 20 #else 21 #include "jsvm.h" 22 #endif 23 24 #include <cstdint> 25 #include <cstdio> 26 #include <cstring> 27 #include <string> 28 29 #include "test_utils.h" 30 31 extern JSVM_Env jsvm_env; 32 33 void PrintException(JSVM_Env env, JSVM_Value exception, const char *call); 34 void CheckErrorAndException(JSVM_Env env, JSVM_Status returnStatus, const char *call); 35 36 #ifndef OHOS_JSVM_XTS 37 #define JSVM_CALL(the_call) \ 38 do { \ 39 JSVM_Status status = (the_call); \ 40 CheckErrorAndException(jsvm_env, status, #the_call); \ 41 } while (0) 42 #endif 43 #define JSVMTEST_CALL(the_call) \ 44 do { \ 45 JSVM_Status status = (the_call); \ 46 CheckErrorAndException(jsvm_env, status, #the_call); \ 47 } while (0) 48 49 // jsvm utils 50 namespace jsvm { 51 52 class HandleScope { 53 public: HandleScope(JSVM_Env jsvmEnv)54 explicit HandleScope(JSVM_Env jsvmEnv) : env(jsvmEnv) 55 { 56 OH_JSVM_OpenHandleScope(env, &scope); 57 } ~HandleScope()58 ~HandleScope() 59 { 60 OH_JSVM_CloseHandleScope(env, scope); 61 } 62 63 private: 64 JSVM_Env env; 65 JSVM_HandleScope scope; 66 }; 67 68 class Value { 69 public: Value(JSVM_Value value)70 explicit Value(JSVM_Value value) : value_(value) {} 71 JSVM_Value()72 operator JSVM_Value() const 73 { 74 return value_; 75 } 76 77 protected: 78 JSVM_Value value_; 79 }; 80 81 class Object : Value { 82 public: Object(JSVM_Value jsvm_value)83 explicit Object(JSVM_Value jsvm_value) : Value(jsvm_value) 84 { 85 bool isObject = false; 86 OH_JSVM_IsObject(jsvm_env, this->value_, &isObject); 87 CHECK_FATAL(isObject, "expected a js object"); 88 } 89 Get(const char * propName)90 JSVM_Value Get(const char *propName) 91 { 92 JSVM_Value result; 93 OH_JSVM_GetNamedProperty(jsvm_env, this->value_, propName, &result); 94 return result; 95 } 96 Set(const char * propName,JSVM_Value propValue)97 void Set(const char *propName, JSVM_Value propValue) 98 { 99 OH_JSVM_SetNamedProperty(jsvm_env, this->value_, propName, propValue); 100 } 101 }; 102 103 // global functions 104 JSVM_Value Str(const char *s); 105 JSVM_Value Str(const std::string &stdString); 106 JSVM_Script Compile(JSVM_Value js_str, const uint8_t *cache = nullptr, size_t size = JSVM_AUTO_LENGTH); 107 JSVM_Script CompileWithName(JSVM_Value js_str, const char *name); 108 JSVM_Script Compile(const char *s, const uint8_t *cache = nullptr, size_t size = JSVM_AUTO_LENGTH); 109 JSVM_Script CompileWithName(const char *s, const char *name); 110 JSVM_Value Run(JSVM_Script script); 111 JSVM_Value Run(const char *s); 112 bool ToBoolean(JSVM_Value val); 113 double ToNumber(JSVM_Value val); 114 std::string ToString(JSVM_Value val); 115 JSVM_Value True(); 116 JSVM_Value False(); 117 JSVM_Value Undefined(); 118 JSVM_Value Null(); 119 JSVM_Value Int32(int32_t v); 120 JSVM_Value Int64(int64_t v); 121 JSVM_Value Uint32(uint32_t v); 122 JSVM_Value Double(double v); 123 JSVM_Value Object(); 124 JSVM_Value Global(); 125 // object property 126 JSVM_Value GetProperty(JSVM_Value object, JSVM_Value key); 127 JSVM_Value GetProperty(JSVM_Value object, const char *name); 128 void SetProperty(JSVM_Value object, JSVM_Value key, JSVM_Value value); 129 void SetProperty(JSVM_Value object, const char *name, JSVM_Value value); 130 // Get named property with name `name` from globalThis 131 JSVM_Value Global(const char *name); 132 // Get property with key `key` from globalThis 133 JSVM_Value Global(JSVM_Value key); 134 // Call function with normal function with empty argument list 135 JSVM_Value Call(JSVM_Value func); 136 // Call function with normal function with specified argument list 137 JSVM_Value Call(JSVM_Value func, JSVM_Value thisArg, std::initializer_list<JSVM_Value> args); 138 // Call function as constructor with empty argument list 139 JSVM_Value New(JSVM_Value constructor); 140 bool InstanceOf(JSVM_Value value, JSVM_Value constructor); 141 bool IsNull(JSVM_Value value); 142 bool IsUndefined(JSVM_Value value); 143 bool IsNullOrUndefined(JSVM_Value value); 144 bool IsBoolean(JSVM_Value value); 145 bool IsTrue(JSVM_Value value); 146 bool IsFalse(JSVM_Value value); 147 bool IsNumber(JSVM_Value value); 148 bool IsString(JSVM_Value value); 149 bool IsObject(JSVM_Value value); 150 bool IsBigInt(JSVM_Value value); 151 bool IsSymbol(JSVM_Value value); 152 bool IsFunction(JSVM_Value value); 153 bool IsArray(JSVM_Value value); 154 bool IsArraybuffer(JSVM_Value value); 155 bool IsPromise(JSVM_Value value); 156 bool IsWasmModuleObject(JSVM_Value value); 157 bool IsWebAssemblyInstance(JSVM_Value value); 158 bool IsWebAssemblyMemory(JSVM_Value value); 159 bool IsWebAssemblyTable(JSVM_Value value); 160 size_t StringLength(JSVM_Value str); 161 uint32_t ArrayLength(JSVM_Value array); 162 JSVM_Value ArrayAt(JSVM_Value array, uint32_t index); 163 JSVM_Value JsonParse(JSVM_Value str); 164 JSVM_Value JsonStringify(JSVM_Value obj); 165 bool Equals(JSVM_Value lhs, JSVM_Value rhs); 166 bool StrictEquals(JSVM_Value lhs, JSVM_Value rhs); 167 // This is a simple log function 168 JSVM_Value MyConsoleLog(JSVM_Env env, JSVM_CallbackInfo info); 169 void InstallMyConsoleLog(JSVM_Env env); 170 void TryTriggerFatalError(JSVM_VM vm); 171 void TryTriggerGC(); 172 } // namespace jsvm 173 174 #endif // JSVM_UTILS_H 175