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
16#include <cstring>
17#include <deque>
18#include <fstream>
19#include <iostream>
20#include <string>
21#include <vector>
22#include <chrono>
23
24#include "gtest/gtest.h"
25#include "jsvm.h"
26#include "jsvm_types.h"
27
28using namespace std;
29using namespace testing;
30using namespace testing::ext;
31
32#define BUF_SIZE 256
33
34static void PrintException(JSVM_Env env, JSVM_Value exception, const char *call)
35{
36    bool isObject = false;
37    JSVM_Status status = OH_JSVM_IsObject(env, exception, &isObject);
38    ASSERT_EQ(status, JSVM_OK);
39    ASSERT_TRUE(isObject);
40
41    JSVM_Value stack;
42    status = OH_JSVM_GetNamedProperty(env, exception, "stack", &stack);
43    ASSERT_EQ(status, JSVM_OK);
44    char stackStr[BUF_SIZE];
45    OH_JSVM_GetValueStringUtf8(env, stack, stackStr, BUF_SIZE, nullptr);
46    printf("[PrintException] exception.stack: %s\n", stackStr);
47
48    status = OH_JSVM_GetNamedProperty(env, exception, "message", &stack);
49    ASSERT_EQ(status, JSVM_OK);
50    char messageStr[BUF_SIZE];
51    OH_JSVM_GetValueStringUtf8(env, stack, messageStr, BUF_SIZE, nullptr);
52    printf("[PrintException] exception.message: %s\n", messageStr);
53}
54
55static void CheckErrorAndException(JSVM_Env env, JSVM_Status returnStatus, const char *call)
56{
57    const JSVM_ExtendedErrorInfo *errorInfo;
58    JSVM_Status status = OH_JSVM_GetLastErrorInfo(env, &errorInfo);
59    ASSERT_EQ(status, JSVM_OK);
60    ASSERT_EQ(returnStatus, errorInfo->errorCode);
61    bool isPending = false;
62    status = OH_JSVM_IsExceptionPending(env, &isPending);
63    ASSERT_EQ(status, JSVM_OK);
64    JSVM_Value exception;
65    status = OH_JSVM_GetAndClearLastException(env, &exception);
66    ASSERT_EQ(status, JSVM_OK);
67    bool isExceptionUndefined = false;
68    status = OH_JSVM_IsUndefined(env, exception, &isExceptionUndefined);
69    ASSERT_EQ(status, JSVM_OK);
70    bool hasException = !isExceptionUndefined;
71    if (hasException) {
72        ASSERT_TRUE(isPending);
73        ASSERT_NE(returnStatus, JSVM_OK);
74        PrintException(env, exception, call);
75        ASSERT_TRUE(false);
76    } else {
77        // no exception
78        ASSERT_FALSE(isPending);
79    }
80    ASSERT_EQ(returnStatus, JSVM_OK);
81}
82
83#define JSVM_CALL(the_call)                              \
84    do {                                                   \
85        JSVM_Status status = (the_call);                     \
86        CheckErrorAndException(env, status, #the_call); \
87    } while (0)
88
89static string srcProf = R"JS(
90function sleep(delay) {
91    var start = (new Date()).getTime();
92    while ((new Date()).getTime() - start < delay) {
93        continue;
94    }
95}
96function work9() {
97    sleep(100);
98}
99function work8() {
100    work9();
101    sleep(100);
102}
103function work7() {
104    work8();
105    sleep(100);
106}
107function work6() {
108    work7();
109    sleep(100);
110}
111function work5() {
112    work6();
113    sleep(100);
114}
115function work4() {
116    work5();
117    sleep(100);
118}
119
120function work3() {
121    work4();
122    sleep(100);
123}
124
125function work2() {
126    work3();
127    sleep(100);
128}
129
130function work1() {
131    work2();
132    sleep(100);
133}
134
135work1();
136)JS";
137
138static JSVM_Value hello_fn(JSVM_Env env, JSVM_CallbackInfo info)
139{
140    JSVM_Value output;
141    void* data = nullptr;
142    OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, nullptr, &data);
143    OH_JSVM_CreateStringUtf8(env, (char*)data, strlen((char*)data), &output);
144    return output;
145}
146
147class Task {
148public:
149    virtual ~Task() = default;
150    virtual void Run() = 0;
151};
152
153static deque<Task*> task_queue;
154
155static JSVM_Value read_fn(JSVM_Env env, JSVM_CallbackInfo info)
156{
157    JSVM_Value promise;
158    JSVM_Deferred deferred;
159    OH_JSVM_CreatePromise(env, &deferred, &promise);
160    class ReadTask : public Task {
161    public:
162        ReadTask(JSVM_Env env, JSVM_Deferred deferred) : env_(env), deferred_(deferred) {}
163        void Run() override
164        {
165            string str;
166            getline(cin, str);
167            JSVM_Value result;
168            OH_JSVM_CreateStringUtf8(env_, str.c_str(), str.size(), &result);
169            OH_JSVM_ResolveDeferred(env_, deferred_, result);
170        }
171    private:
172        JSVM_Env env_;
173        JSVM_Deferred deferred_;
174    };
175    task_queue.push_back(new ReadTask(env, deferred));
176    return promise;
177}
178
179#define BUF_SIZE1 102400
180static JSVM_Value print_fn(JSVM_Env env, JSVM_CallbackInfo info)
181{
182    size_t argc = 1;
183    JSVM_Value argv[1];
184    OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
185    if (argc > 0) {
186        char buf[BUF_SIZE1];
187        OH_JSVM_GetValueStringUtf8(env, argv[0], buf, BUF_SIZE1, nullptr);
188        std::cout << buf << std::endl;
189    }
190    return nullptr;
191}
192
193static JSVM_CallbackStruct hello_cb = { hello_fn, (void *)"Hello" };
194static JSVM_CallbackStruct read_cb = { read_fn, nullptr };
195static JSVM_CallbackStruct print_cb = { print_fn, nullptr };
196
197static JSVM_PropertyDescriptor property_descriptors[] = {
198    {
199        "hello",
200        NULL,
201        &hello_cb,
202        NULL,
203        NULL,
204        NULL,
205        JSVM_DEFAULT
206    },
207    {
208        "read",
209        NULL,
210        &read_cb,
211        NULL,
212        NULL,
213        NULL,
214        JSVM_DEFAULT
215    },
216    {
217        "print",
218        NULL,
219        &print_cb,
220        NULL,
221        NULL,
222        NULL,
223        JSVM_DEFAULT
224    },
225};
226
227class JSVMTest : public testing::Test {
228public:
229    static void SetUpTestCase()
230    {
231        GTEST_LOG_(INFO) << "JSVMTest SetUpTestCase";
232        JSVM_InitOptions init_options{};
233        OH_JSVM_Init(&init_options);
234    }
235
236    static void TearDownTestCase()
237    {
238        GTEST_LOG_(INFO) << "JSVMTest TearDownTestCase";
239    }
240
241    void SetUp() override
242    {
243        GTEST_LOG_(INFO) << "JSVMTest SetUp";
244        OH_JSVM_CreateVM(nullptr, &vm);
245        // propertyCount is 3
246        OH_JSVM_CreateEnv(vm, 3, property_descriptors, &env);
247        OH_JSVM_OpenVMScope(vm, &vm_scope);
248        OH_JSVM_OpenEnvScope(env, &env_scope);
249        OH_JSVM_OpenHandleScope(env, &handleScope);
250    }
251    void TearDown() override
252    {
253        GTEST_LOG_(INFO) << "JSVMTest TearDown";
254        OH_JSVM_CloseHandleScope(env, handleScope);
255        OH_JSVM_CloseEnvScope(env, env_scope);
256        OH_JSVM_CloseVMScope(vm, vm_scope);
257        OH_JSVM_DestroyEnv(env);
258        OH_JSVM_DestroyVM(vm);
259    }
260
261protected:
262    JSVM_Env env = nullptr;
263    JSVM_VM vm = nullptr;
264    JSVM_EnvScope env_scope = nullptr;
265    JSVM_VMScope vm_scope = nullptr;
266    JSVM_HandleScope handleScope;
267};
268
269HWTEST_F(JSVMTest, JSVMGetVersion001, TestSize.Level1)
270{
271    uint32_t versionId = 0;
272    JSVM_CALL(OH_JSVM_GetVersion(env, &versionId));
273    ASSERT_EQ(versionId, 9);
274}
275
276HWTEST_F(JSVMTest, JSVMEquals001, TestSize.Level1)
277{
278    JSVM_Value lhs = nullptr;
279    bool x = true;
280    JSVM_CALL(OH_JSVM_GetBoolean(env, x, &lhs));
281    JSVM_Value rhs = nullptr;
282    bool y = true;
283    JSVM_CALL(OH_JSVM_GetBoolean(env, y, &rhs));
284    bool isEquals = false;
285    JSVM_CALL(OH_JSVM_Equals(env, lhs, rhs, &isEquals));
286    ASSERT_TRUE(isEquals);
287}
288
289HWTEST_F(JSVMTest, JSVMCreateCodeCache001, TestSize.Level1)
290{
291    JSVM_Value jsSrc;
292    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, srcProf.c_str(), srcProf.size(), &jsSrc));
293
294    const uint8_t x1 = 34;
295    const uint8_t* x2 = &x1;
296    const uint8_t** dataPtr = &x2;
297    size_t x3 = 1;
298    size_t* lengthPtr = &x3;
299    JSVM_Script script = nullptr;
300    JSVM_CALL(OH_JSVM_CompileScript(env, jsSrc, nullptr, 0, true, nullptr, &script));
301    JSVM_CALL(OH_JSVM_CreateCodeCache(env, script, dataPtr, lengthPtr));
302}
303
304HWTEST_F(JSVMTest, JSVMAcquire001, TestSize.Level1)
305{
306    JSVM_CALL(OH_JSVM_AcquireLock(env));
307}
308
309HWTEST_F(JSVMTest, JSVMIsObject001, TestSize.Level1)
310{
311    JSVM_Value obj;
312    JSVM_CALL(OH_JSVM_CreateMap(env, &obj));
313    bool result = false;
314    JSVM_CALL(OH_JSVM_IsObject(env, obj, &result));
315    ASSERT_TRUE(result);
316
317    JSVM_CALL(OH_JSVM_CreateSymbol(env, nullptr, &obj));
318    result = false;
319    JSVM_CALL(OH_JSVM_IsObject(env, obj, &result));
320    ASSERT_FALSE(result);
321}
322
323HWTEST_F(JSVMTest, JSVMIsBoolean001, TestSize.Level1)
324{
325    JSVM_Value obj;
326    JSVM_CALL(OH_JSVM_CreateArray(env, &obj));
327    bool result = false;
328    JSVM_CALL(OH_JSVM_IsBoolean(env, obj, &result));
329    ASSERT_FALSE(result);
330
331    bool boolvalue = true;
332    JSVM_CALL(OH_JSVM_GetBoolean(env, boolvalue, &obj));
333    result = false;
334    JSVM_CALL(OH_JSVM_IsBoolean(env, obj, &result));
335    ASSERT_TRUE(result);
336}
337
338HWTEST_F(JSVMTest, JSVMIsString001, TestSize.Level1)
339{
340    JSVM_Value createString;
341    char str[12] = "hello world";
342    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, str, 12, &createString));
343    bool result = false;
344    JSVM_CALL(OH_JSVM_IsString(env, createString, &result));
345    ASSERT_TRUE(result);
346
347    JSVM_Value obj = nullptr;
348    JSVM_CALL(OH_JSVM_CreateSet(env, &obj));
349    result = false;
350    JSVM_CALL(OH_JSVM_IsString(env, obj, &result));
351    ASSERT_FALSE(result);
352}
353
354
355HWTEST_F(JSVMTest, JSVMIsFunction001, TestSize.Level1)
356{
357    JSVM_Value function;
358    JSVM_CallbackStruct param;
359    param.data = nullptr;
360    param.callback = nullptr;
361    JSVM_CALL(OH_JSVM_CreateFunction(env, "func", JSVM_AUTO_LENGTH, &param, &function));
362    bool result = false;
363    JSVM_CALL(OH_JSVM_IsFunction(env, function, &result));
364    ASSERT_TRUE(result);
365
366    JSVM_Value obj;
367    JSVM_CALL(OH_JSVM_CreateObject(env, &obj));
368    result = false;
369    JSVM_CALL(OH_JSVM_IsFunction(env, obj, &result));
370    ASSERT_FALSE(result);
371}
372
373HWTEST_F(JSVMTest, JSVMIsSymbol001, TestSize.Level1)
374{
375    JSVM_Value obj;
376    JSVM_CALL(OH_JSVM_CreateSymbol(env, nullptr, &obj));
377    bool result = false;
378    JSVM_CALL(OH_JSVM_IsSymbol(env, obj, &result));
379    ASSERT_TRUE(result);
380
381    JSVM_CALL(OH_JSVM_CreateObject(env, &obj));
382    result = false;
383    JSVM_CALL(OH_JSVM_IsSymbol(env, obj, &result));
384    ASSERT_FALSE(result);
385}
386
387HWTEST_F(JSVMTest, JSVMIsNumber001, TestSize.Level1)
388{
389    JSVM_Value obj;
390    JSVM_CALL(OH_JSVM_CreateObject(env, &obj));
391    bool result = false;
392    JSVM_CALL(OH_JSVM_IsNumber(env, obj, &result));
393    ASSERT_FALSE(result);
394
395    JSVM_Value value;
396    int intValue = 2;
397    JSVM_CALL(OH_JSVM_CreateInt32(env, intValue, &value));
398    result = false;
399    JSVM_CALL(OH_JSVM_IsNumber(env, value, &result));
400    ASSERT_TRUE(result);
401}
402
403HWTEST_F(JSVMTest, JSVMIsBigInt001, TestSize.Level1)
404{
405    JSVM_Value obj;
406    JSVM_CALL(OH_JSVM_CreateObject(env, &obj));
407    bool result = false;
408    JSVM_CALL(OH_JSVM_IsBigInt(env, obj, &result));
409    ASSERT_FALSE(result);
410
411    JSVM_Value bigint;
412    int intValue = 2;
413    JSVM_CALL(OH_JSVM_CreateBigintInt64(env, intValue, &bigint));
414    result = false;
415    JSVM_CALL(OH_JSVM_IsBigInt(env, bigint, &result));
416    ASSERT_TRUE(result);
417}
418
419HWTEST_F(JSVMTest, JSVMIsNull001, TestSize.Level1)
420{
421    JSVM_Value input;
422    bool result = false;
423    JSVM_CALL(OH_JSVM_CreateArray(env, &input));
424    JSVM_CALL(OH_JSVM_IsNull(env, input, &result));
425    ASSERT_FALSE(result);
426
427    JSVM_Value input2;
428    bool result2;
429    JSVM_CALL(OH_JSVM_GetNull(env, &input2));
430    JSVM_CALL(OH_JSVM_IsNull(env, input2, &result2));
431    ASSERT_TRUE(result2);
432}
433
434HWTEST_F(JSVMTest, JSVMIsUndefined001, TestSize.Level1)
435{
436    JSVM_Value input;
437    bool result = false;
438    JSVM_CALL(OH_JSVM_CreateArray(env, &input));
439    JSVM_CALL(OH_JSVM_IsUndefined(env, input, &result));
440    ASSERT_FALSE(result);
441
442    JSVM_Value input2;
443    bool result2;
444    JSVM_CALL(OH_JSVM_GetUndefined(env, &input2));
445    JSVM_CALL(OH_JSVM_IsUndefined(env, input2, &result2));
446    ASSERT_TRUE(result2);
447}
448
449HWTEST_F(JSVMTest, OH_JSVM_IsNullOrUndefined001, TestSize.Level1)
450{
451    JSVM_Value input;
452    bool result = false;
453    JSVM_CALL(OH_JSVM_CreateArray(env, &input));
454    JSVM_CALL(OH_JSVM_IsNullOrUndefined(env, input, &result));
455    ASSERT_FALSE(result);
456
457    JSVM_Value input2;
458    bool result2 = false;
459    JSVM_CALL(OH_JSVM_GetNull(env, &input2));
460    JSVM_CALL(OH_JSVM_IsNullOrUndefined(env, input2, &result2));
461    ASSERT_TRUE(result2);
462
463    result2 = false;
464    JSVM_CALL(OH_JSVM_GetUndefined(env, &input2));
465    JSVM_CALL(OH_JSVM_IsNullOrUndefined(env, input2, &result2));
466    ASSERT_TRUE(result2);
467}
468
469HWTEST_F(JSVMTest, JSVMIsLocked001, TestSize.Level1)
470{
471    bool isLocked = false;
472    JSVM_CALL(OH_JSVM_IsLocked(env, &isLocked));
473}
474
475HWTEST_F(JSVMTest, JSVMReleaseLock001, TestSize.Level1)
476{
477    bool isLocked = false;
478    JSVM_CALL(OH_JSVM_IsLocked(env, &isLocked));
479    JSVM_CALL(OH_JSVM_ReleaseLock(env));
480}
481
482HWTEST_F(JSVMTest, JSVMCompileScriptWithOrigin001, TestSize.Level1)
483{
484    JSVM_Value jsSrc;
485    JSVM_CALL(OH_JSVM_CreateStringUtf8(env, srcProf.c_str(), srcProf.size(), &jsSrc));
486    JSVM_Script script;
487    JSVM_ScriptOrigin origin {
488        .sourceMapUrl = "/data/local/tmp/workload/index.js.map",
489        // 源文件名字
490        .resourceName = "index.js",
491        // scirpt 在源文件中的起始行列号
492        .resourceLineOffset = 0,
493        .resourceColumnOffset = 0,
494    };
495    JSVM_CALL(OH_JSVM_CompileScriptWithOrigin(env, jsSrc, nullptr, 0, true, nullptr, &origin, &script));
496}
497
498HWTEST_F(JSVMTest, JSVMCompileScriptWithOrigin002, TestSize.Level1)
499{
500    JSVM_Value jsSrc = nullptr;
501    bool x = true;
502    JSVM_CALL(OH_JSVM_GetBoolean(env, x, &jsSrc));
503    JSVM_Script script;
504    JSVM_ScriptOrigin origin {
505        .sourceMapUrl = "/data/local/tmp/workload/index.js.map",
506        // 源文件名字
507        .resourceName = "index.js",
508        // scirpt 在源文件中的起始行列号
509        .resourceLineOffset = 0,
510        .resourceColumnOffset = 0,
511    };
512    JSVM_Status status = OH_JSVM_CompileScriptWithOrigin(env, jsSrc, nullptr, 0, true, nullptr, &origin, &script);
513    ASSERT_EQ(status, 3);
514}
515
516HWTEST_F(JSVMTest, JSVMCompileScriptWithOrigin003, TestSize.Level1)
517{
518    JSVM_Value jsSrc = nullptr;
519    bool x = true;
520    JSVM_CALL(OH_JSVM_GetBoolean(env, x, &jsSrc));
521    JSVM_ScriptOrigin origin {
522        .sourceMapUrl = "/data/local/tmp/workload/index.js.map",
523        // 源文件名字
524        .resourceName = "index.js",
525        // scirpt 在源文件中的起始行列号
526        .resourceLineOffset = 0,
527        .resourceColumnOffset = 0,
528    };
529    JSVM_Status status = OH_JSVM_CompileScriptWithOrigin(env, jsSrc, nullptr, 0, true, nullptr, &origin, nullptr);
530    ASSERT_EQ(status, 1);
531}
532
533static JSVM_PropertyHandlerConfigurationStruct propertyCfg{
534    nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
535};
536
537HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler001, TestSize.Level1)
538{
539    JSVM_CallbackStruct param;
540    param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
541        JSVM_Value thisVar = nullptr;
542        JSVM_CALL(OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr));
543        return thisVar;
544    };
545    param.data = nullptr;
546    JSVM_Value testWrapClass = nullptr;
547    JSVM_CALL(OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", 5, &param, 0, nullptr,
548                                                    &propertyCfg, nullptr, &testWrapClass));
549}
550
551HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler002, TestSize.Level1)
552{
553    JSVM_CallbackStruct param;
554    param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
555        JSVM_Value thisVar = nullptr;
556        JSVM_CALL(OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr));
557        return thisVar;
558    };
559    param.data = nullptr;
560    JSVM_Value testWrapClass = nullptr;
561    JSVM_CALL(OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", JSVM_AUTO_LENGTH, &param, 0,
562                                                    nullptr, &propertyCfg, nullptr, &testWrapClass));
563}
564
565HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler003, TestSize.Level1)
566{
567    JSVM_CallbackStruct param;
568    param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
569        JSVM_Value thisVar = nullptr;
570        JSVM_CALL(OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr));
571        return thisVar;
572    };
573    param.data = nullptr;
574    JSVM_Value testWrapClass = nullptr;
575    JSVM_CALL(OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", 4, &param, 0, nullptr, &propertyCfg,
576                                                    nullptr, &testWrapClass));
577}
578
579HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler004, TestSize.Level1)
580{
581    JSVM_CallbackStruct param;
582    param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
583        JSVM_Value thisVar = nullptr;
584        JSVM_CALL(OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr));
585        return thisVar;
586    };
587    param.data = nullptr;
588    JSVM_Value testWrapClass = nullptr;
589    JSVM_CALL(OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", 6, &param, 0, nullptr,
590                                                    &propertyCfg, nullptr, &testWrapClass));
591}
592
593HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler005, TestSize.Level1)
594{
595    JSVM_CallbackStruct param;
596    param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
597        JSVM_Value thisVar = nullptr;
598        JSVM_CALL(OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr));
599        return thisVar;
600    };
601    param.data = nullptr;
602    JSVM_Status status = OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", JSVM_AUTO_LENGTH, &param, 0,
603                                                                nullptr, &propertyCfg, nullptr, nullptr);
604    ASSERT_EQ(status, JSVM_INVALID_ARG);
605}
606
607HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler006, TestSize.Level1)
608{
609    JSVM_Value testWrapClass = nullptr;
610    JSVM_Status status = OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", JSVM_AUTO_LENGTH, nullptr, 0,
611                                                                nullptr, &propertyCfg, nullptr, &testWrapClass);
612    ASSERT_EQ(status, JSVM_INVALID_ARG);
613}
614
615HWTEST_F(JSVMTest, JSVMDefineClassWithPropertyHandler007, TestSize.Level1)
616{
617    JSVM_CallbackStruct param;
618    param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
619        JSVM_Value thisVar = nullptr;
620        JSVM_CALL(OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr));
621        return thisVar;
622    };
623    param.data = nullptr;
624    JSVM_Value testWrapClass = nullptr;
625    JSVM_Status status = OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", JSVM_AUTO_LENGTH, &param, 0,
626                                                                nullptr, nullptr, nullptr, &testWrapClass);
627    ASSERT_EQ(status, JSVM_INVALID_ARG);
628}
629
630HWTEST_F(JSVMTest, JSVMCreateSnapshot001, TestSize.Level1)
631{
632    const char *blobData = nullptr;
633    size_t blobSize = 0;
634    JSVM_Env envs[1] = {env};
635    JSVM_Status status = OH_JSVM_CreateSnapshot(vm, 1, envs, &blobData, &blobSize);
636    ASSERT_EQ(status, JSVM_GENERIC_FAILURE);
637}
638
639HWTEST_F(JSVMTest, JSVMCreateEnvFromSnapshot001, TestSize.Level1)
640{
641    JSVM_Env env2 = nullptr;
642    JSVM_Status status = OH_JSVM_CreateEnvFromSnapshot(vm, 0, &env2);
643    ASSERT_EQ(status, JSVM_GENERIC_FAILURE);
644}