• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstring>
16 #include <ostream>
17 
18 #include "jsvm.h"
19 #include "jsvm_types.h"
20 #include "jsvm_utils.h"
21 #include "jsvmtest.h"
22 #include "test_utils.h"
23 
TEST(test_script_string_latin1)24 TEST(test_script_string_latin1)
25 {
26     char src[] = "a = 100; a = a + 1";
27     JSVM_Value str;
28     bool copied = false;
29     JSVMTEST_CALL(OH_JSVM_CreateExternalStringLatin1(env, src, strlen(src), nullptr, nullptr, &str, &copied));
30     auto result = jsvm::Run(jsvm::Compile(str));
31     CHECK(jsvm::ToNumber(result) == 101);
32     CHECK(copied == false);
33 }
34 
35 // would probably success but is not well defined
TEST(test_script_string_latin1_modified)36 TEST(test_script_string_latin1_modified)
37 {
38     char src[] = "a = 100; a = a + 1";
39     JSVM_Value str;
40     bool copied = false;
41     JSVMTEST_CALL(OH_JSVM_CreateExternalStringLatin1(env, src, strlen(src), nullptr, nullptr, &str, &copied));
42     src[4] = '2';
43     auto result = jsvm::Run(jsvm::Compile(str));
44     CHECK(jsvm::ToNumber(result) == 201);
45     CHECK(copied == false);
46 }
47 
TEST(test_script_string_latin1_chinese)48 TEST(test_script_string_latin1_chinese)
49 {
50     char src[] = "a = '一'; a = a + '二'";
51     JSVM_Value str;
52     bool copied = false;
53     JSVMTEST_CALL(OH_JSVM_CreateExternalStringLatin1(env, src, strlen(src), nullptr, nullptr, &str, &copied));
54     auto result = jsvm::Run(jsvm::Compile(str));
55     CHECK(jsvm::ToString(result) != "一二");
56     CHECK(copied == false);
57 }
58 
LocalFinalize(JSVM_Env env,void * finalizeData,void * finalizeHint)59 static void LocalFinalize(JSVM_Env env, void *finalizeData, void *finalizeHint)
60 {
61     const int hint = 4;
62     CHECK(*static_cast<int *>(finalizeHint) == hint);
63     delete static_cast<int *>(finalizeHint);
64 }
65 
TEST(test_string_callback)66 TEST(test_string_callback)
67 {
68     char src[] = "a = 100; a = a + 1";
69     JSVM_Value str;
70     bool copied = false;
71     auto hint = new int(4);
72     {
73         JSVM_HandleScope scope;
74         JSVMTEST_CALL(OH_JSVM_CreateExternalStringLatin1(env, src, strlen(src), LocalFinalize, hint, &str, &copied));
75         auto result = jsvm::Run(jsvm::Compile(str));
76         CHECK(jsvm::ToNumber(result) == 101);
77     }
78     CHECK(copied == false);
79 }
80 
TEST(test_script_string_utf16)81 TEST(test_script_string_utf16)
82 {
83     char16_t src[] = u"a = 'hello'; a = a + ' world'";
84     JSVM_Value str;
85     bool copied = false;
86     JSVMTEST_CALL(OH_JSVM_CreateExternalStringUtf16(env, src, std::char_traits<char16_t>::length(src), nullptr, nullptr,
87                                                     &str, &copied));
88     auto result = jsvm::Run(jsvm::Compile(str));
89     CHECK(jsvm::ToString(result) == "hello world");
90     CHECK(copied == false);
91 }
92 
TEST(test_script_string_utf16_chinese)93 TEST(test_script_string_utf16_chinese)
94 {
95     char16_t src[] = u"a = '一'; a = a + '二'";
96     JSVM_Value str;
97     bool copied = false;
98     JSVMTEST_CALL(OH_JSVM_CreateExternalStringUtf16(env, src, std::char_traits<char16_t>::length(src), nullptr, nullptr,
99                                                     &str, &copied));
100     auto result = jsvm::Run(jsvm::Compile(str));
101     CHECK(jsvm::ToString(result) == "一二");
102     CHECK(copied == false);
103 }