29 |
30 | #include "quickjs.h"
31 |
32 | JSModuleDef *js_init_module_std(JSContext *ctx, const char *module_name);
33 | JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name);
34 | void js_std_add_helpers(JSContext *ctx, int argc, char **argv);
35 | void js_std_loop(JSContext *ctx);
36 | void js_std_free_handlers(JSRuntime *rt);
37 | void js_std_dump_error(JSContext *ctx);
38 | uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename);
39 | int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
40 | JS_BOOL use_realpath, JS_BOOL is_main);
41 | JSModuleDef *js_module_loader(JSContext *ctx,
42 | const char *module_name, void *opaque);
43 | void js_std_eval_binary(JSContext *ctx, const uint8_t *buf, size_t buf_len,
44 | int flags);
45 | void js_std_promise_rejection_tracker(JSContext *ctx, JSValueConst promise,
46 | JSValueConst reason,
47 | JS_BOOL is_handled, void *opaque);
48 |
49 | #endif /* QUICKJS_LIBC_H */
50 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/quickjs-opcode.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS opcode definitions
3 | *
4 | * Copyright (c) 2017-2018 Fabrice Bellard
5 | * Copyright (c) 2017-2018 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifdef FMT
27 | FMT(none)
28 | FMT(none_int)
29 | FMT(none_loc)
30 | FMT(none_arg)
31 | FMT(none_var_ref)
32 | FMT(u8)
33 | FMT(i8)
34 | FMT(loc8)
35 | FMT(const8)
36 | FMT(label8)
37 | FMT(u16)
38 | FMT(i16)
39 | FMT(label16)
40 | FMT(npop)
41 | FMT(npopx)
42 | FMT(npop_u16)
43 | FMT(loc)
44 | FMT(arg)
45 | FMT(var_ref)
46 | FMT(u32)
47 | FMT(i32)
48 | FMT(const)
49 | FMT(label)
50 | FMT(atom)
51 | FMT(atom_u8)
52 | FMT(atom_u16)
53 | FMT(atom_label_u8)
54 | FMT(atom_label_u16)
55 | FMT(label_u16)
56 | #undef FMT
57 | #endif /* FMT */
58 |
59 | #ifdef DEF
60 |
61 | #ifndef def
62 | #define def(id, size, n_pop, n_push, f) DEF(id, size, n_pop, n_push, f)
63 | #endif
64 |
65 | DEF(invalid, 1, 0, 0, none) /* never emitted */
66 |
67 | /* push values */
68 | DEF( push_i32, 5, 0, 1, i32)
69 | DEF( push_const, 5, 0, 1, const)
70 | DEF( fclosure, 5, 0, 1, const) /* must follow push_const */
71 | DEF(push_atom_value, 5, 0, 1, atom)
72 | DEF( private_symbol, 5, 0, 1, atom)
73 | DEF( undefined, 1, 0, 1, none)
74 | DEF( null, 1, 0, 1, none)
75 | DEF( push_this, 1, 0, 1, none) /* only used at the start of a function */
76 | DEF( push_false, 1, 0, 1, none)
77 | DEF( push_true, 1, 0, 1, none)
78 | DEF( object, 1, 0, 1, none)
79 | DEF( special_object, 2, 0, 1, u8) /* only used at the start of a function */
80 | DEF( rest, 3, 0, 1, u16) /* only used at the start of a function */
81 |
82 | DEF( drop, 1, 1, 0, none) /* a -> */
83 | DEF( nip, 1, 2, 1, none) /* a b -> b */
84 | DEF( nip1, 1, 3, 2, none) /* a b c -> b c */
85 | DEF( dup, 1, 1, 2, none) /* a -> a a */
86 | DEF( dup1, 1, 2, 3, none) /* a b -> a a b */
87 | DEF( dup2, 1, 2, 4, none) /* a b -> a b a b */
88 | DEF( dup3, 1, 3, 6, none) /* a b c -> a b c a b c */
89 | DEF( insert2, 1, 2, 3, none) /* obj a -> a obj a (dup_x1) */
90 | DEF( insert3, 1, 3, 4, none) /* obj prop a -> a obj prop a (dup_x2) */
91 | DEF( insert4, 1, 4, 5, none) /* this obj prop a -> a this obj prop a */
92 | DEF( perm3, 1, 3, 3, none) /* obj a b -> a obj b */
93 | DEF( perm4, 1, 4, 4, none) /* obj prop a b -> a obj prop b */
94 | DEF( perm5, 1, 5, 5, none) /* this obj prop a b -> a this obj prop b */
95 | DEF( swap, 1, 2, 2, none) /* a b -> b a */
96 | DEF( swap2, 1, 4, 4, none) /* a b c d -> c d a b */
97 | DEF( rot3l, 1, 3, 3, none) /* x a b -> a b x */
98 | DEF( rot3r, 1, 3, 3, none) /* a b x -> x a b */
99 | DEF( rot4l, 1, 4, 4, none) /* x a b c -> a b c x */
100 | DEF( rot5l, 1, 5, 5, none) /* x a b c d -> a b c d x */
101 |
102 | DEF(call_constructor, 3, 2, 1, npop) /* func new.target args -> ret. arguments are not counted in n_pop */
103 | DEF( call, 3, 1, 1, npop) /* arguments are not counted in n_pop */
104 | DEF( tail_call, 3, 1, 0, npop) /* arguments are not counted in n_pop */
105 | DEF( call_method, 3, 2, 1, npop) /* arguments are not counted in n_pop */
106 | DEF(tail_call_method, 3, 2, 0, npop) /* arguments are not counted in n_pop */
107 | DEF( array_from, 3, 0, 1, npop) /* arguments are not counted in n_pop */
108 | DEF( apply, 3, 3, 1, u16)
109 | DEF( return, 1, 1, 0, none)
110 | DEF( return_undef, 1, 0, 0, none)
111 | DEF(check_ctor_return, 1, 1, 2, none)
112 | DEF( check_ctor, 1, 0, 0, none)
113 | DEF( check_brand, 1, 2, 2, none) /* this_obj func -> this_obj func */
114 | DEF( add_brand, 1, 2, 0, none) /* this_obj home_obj -> */
115 | DEF( return_async, 1, 1, 0, none)
116 | DEF( throw, 1, 1, 0, none)
117 | DEF( throw_var, 6, 0, 0, atom_u8)
118 | DEF( eval, 5, 1, 1, npop_u16) /* func args... -> ret_val */
119 | DEF( apply_eval, 3, 2, 1, u16) /* func array -> ret_eval */
120 | DEF( regexp, 1, 2, 1, none) /* create a RegExp object from the pattern and a
121 | bytecode string */
122 | DEF( get_super, 1, 1, 1, none)
123 | DEF( import, 1, 1, 1, none) /* dynamic module import */
124 |
125 | DEF( check_var, 5, 0, 1, atom) /* check if a variable exists */
126 | DEF( get_var_undef, 5, 0, 1, atom) /* push undefined if the variable does not exist */
127 | DEF( get_var, 5, 0, 1, atom) /* throw an exception if the variable does not exist */
128 | DEF( put_var, 5, 1, 0, atom) /* must come after get_var */
129 | DEF( put_var_init, 5, 1, 0, atom) /* must come after put_var. Used to initialize a global lexical variable */
130 | DEF( put_var_strict, 5, 2, 0, atom) /* for strict mode variable write */
131 |
132 | DEF( get_ref_value, 1, 2, 3, none)
133 | DEF( put_ref_value, 1, 3, 0, none)
134 |
135 | DEF( define_var, 6, 0, 0, atom_u8)
136 | DEF(check_define_var, 6, 0, 0, atom_u8)
137 | DEF( define_func, 6, 1, 0, atom_u8)
138 | DEF( get_field, 5, 1, 1, atom)
139 | DEF( get_field2, 5, 1, 2, atom)
140 | DEF( put_field, 5, 2, 0, atom)
141 | DEF( get_private_field, 1, 2, 1, none) /* obj prop -> value */
142 | DEF( put_private_field, 1, 3, 0, none) /* obj value prop -> */
143 | DEF(define_private_field, 1, 3, 1, none) /* obj prop value -> obj */
144 | DEF( get_array_el, 1, 2, 1, none)
145 | DEF( get_array_el2, 1, 2, 2, none) /* obj prop -> obj value */
146 | DEF( put_array_el, 1, 3, 0, none)
147 | DEF(get_super_value, 1, 3, 1, none) /* this obj prop -> value */
148 | DEF(put_super_value, 1, 4, 0, none) /* this obj prop value -> */
149 | DEF( define_field, 5, 2, 1, atom)
150 | DEF( set_name, 5, 1, 1, atom)
151 | DEF(set_name_computed, 1, 2, 2, none)
152 | DEF( set_proto, 1, 2, 1, none)
153 | DEF(set_home_object, 1, 2, 2, none)
154 | DEF(define_array_el, 1, 3, 2, none)
155 | DEF( append, 1, 3, 2, none) /* append enumerated object, update length */
156 | DEF(copy_data_properties, 2, 3, 3, u8)
157 | DEF( define_method, 6, 2, 1, atom_u8)
158 | DEF(define_method_computed, 2, 3, 1, u8) /* must come after define_method */
159 | DEF( define_class, 6, 2, 2, atom_u8) /* parent ctor -> ctor proto */
160 | DEF( define_class_computed, 6, 3, 3, atom_u8) /* field_name parent ctor -> field_name ctor proto (class with computed name) */
161 |
162 | DEF( get_loc, 3, 0, 1, loc)
163 | DEF( put_loc, 3, 1, 0, loc) /* must come after get_loc */
164 | DEF( set_loc, 3, 1, 1, loc) /* must come after put_loc */
165 | DEF( get_arg, 3, 0, 1, arg)
166 | DEF( put_arg, 3, 1, 0, arg) /* must come after get_arg */
167 | DEF( set_arg, 3, 1, 1, arg) /* must come after put_arg */
168 | DEF( get_var_ref, 3, 0, 1, var_ref)
169 | DEF( put_var_ref, 3, 1, 0, var_ref) /* must come after get_var_ref */
170 | DEF( set_var_ref, 3, 1, 1, var_ref) /* must come after put_var_ref */
171 | DEF(set_loc_uninitialized, 3, 0, 0, loc)
172 | DEF( get_loc_check, 3, 0, 1, loc)
173 | DEF( put_loc_check, 3, 1, 0, loc) /* must come after get_loc_check */
174 | DEF( put_loc_check_init, 3, 1, 0, loc)
175 | DEF(get_var_ref_check, 3, 0, 1, var_ref)
176 | DEF(put_var_ref_check, 3, 1, 0, var_ref) /* must come after get_var_ref_check */
177 | DEF(put_var_ref_check_init, 3, 1, 0, var_ref)
178 | DEF( close_loc, 3, 0, 0, loc)
179 | DEF( if_false, 5, 1, 0, label)
180 | DEF( if_true, 5, 1, 0, label) /* must come after if_false */
181 | DEF( goto, 5, 0, 0, label) /* must come after if_true */
182 | DEF( catch, 5, 0, 1, label)
183 | DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */
184 | DEF( ret, 1, 1, 0, none) /* used to return from the finally block */
185 |
186 | DEF( to_object, 1, 1, 1, none)
187 | //DEF( to_string, 1, 1, 1, none)
188 | DEF( to_propkey, 1, 1, 1, none)
189 | DEF( to_propkey2, 1, 2, 2, none)
190 |
191 | DEF( with_get_var, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
192 | DEF( with_put_var, 10, 2, 1, atom_label_u8) /* must be in the same order as scope_xxx */
193 | DEF(with_delete_var, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
194 | DEF( with_make_ref, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
195 | DEF( with_get_ref, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
196 | DEF(with_get_ref_undef, 10, 1, 0, atom_label_u8)
197 |
198 | DEF( make_loc_ref, 7, 0, 2, atom_u16)
199 | DEF( make_arg_ref, 7, 0, 2, atom_u16)
200 | DEF(make_var_ref_ref, 7, 0, 2, atom_u16)
201 | DEF( make_var_ref, 5, 0, 2, atom)
202 |
203 | DEF( for_in_start, 1, 1, 1, none)
204 | DEF( for_of_start, 1, 1, 3, none)
205 | DEF(for_await_of_start, 1, 1, 3, none)
206 | DEF( for_in_next, 1, 1, 3, none)
207 | DEF( for_of_next, 2, 3, 5, u8)
208 | DEF(for_await_of_next, 1, 3, 4, none)
209 | DEF(iterator_get_value_done, 1, 1, 2, none)
210 | DEF( iterator_close, 1, 3, 0, none)
211 | DEF(iterator_close_return, 1, 4, 4, none)
212 | DEF(async_iterator_close, 1, 3, 2, none)
213 | DEF(async_iterator_next, 1, 4, 4, none)
214 | DEF(async_iterator_get, 2, 4, 5, u8)
215 | DEF( initial_yield, 1, 0, 0, none)
216 | DEF( yield, 1, 1, 2, none)
217 | DEF( yield_star, 1, 2, 2, none)
218 | DEF(async_yield_star, 1, 1, 2, none)
219 | DEF( await, 1, 1, 1, none)
220 |
221 | /* arithmetic/logic operations */
222 | DEF( neg, 1, 1, 1, none)
223 | DEF( plus, 1, 1, 1, none)
224 | DEF( dec, 1, 1, 1, none)
225 | DEF( inc, 1, 1, 1, none)
226 | DEF( post_dec, 1, 1, 2, none)
227 | DEF( post_inc, 1, 1, 2, none)
228 | DEF( dec_loc, 2, 0, 0, loc8)
229 | DEF( inc_loc, 2, 0, 0, loc8)
230 | DEF( add_loc, 2, 1, 0, loc8)
231 | DEF( not, 1, 1, 1, none)
232 | DEF( lnot, 1, 1, 1, none)
233 | DEF( typeof, 1, 1, 1, none)
234 | DEF( delete, 1, 2, 1, none)
235 | DEF( delete_var, 5, 0, 1, atom)
236 |
237 | DEF( mul, 1, 2, 1, none)
238 | DEF( div, 1, 2, 1, none)
239 | DEF( mod, 1, 2, 1, none)
240 | DEF( add, 1, 2, 1, none)
241 | DEF( sub, 1, 2, 1, none)
242 | DEF( pow, 1, 2, 1, none)
243 | DEF( shl, 1, 2, 1, none)
244 | DEF( sar, 1, 2, 1, none)
245 | DEF( shr, 1, 2, 1, none)
246 | DEF( lt, 1, 2, 1, none)
247 | DEF( lte, 1, 2, 1, none)
248 | DEF( gt, 1, 2, 1, none)
249 | DEF( gte, 1, 2, 1, none)
250 | DEF( instanceof, 1, 2, 1, none)
251 | DEF( in, 1, 2, 1, none)
252 | DEF( eq, 1, 2, 1, none)
253 | DEF( neq, 1, 2, 1, none)
254 | DEF( strict_eq, 1, 2, 1, none)
255 | DEF( strict_neq, 1, 2, 1, none)
256 | DEF( and, 1, 2, 1, none)
257 | DEF( xor, 1, 2, 1, none)
258 | DEF( or, 1, 2, 1, none)
259 | DEF(is_undefined_or_null, 1, 1, 1, none)
260 | #ifdef CONFIG_BIGNUM
261 | DEF( mul_pow10, 1, 2, 1, none)
262 | DEF( math_mod, 1, 2, 1, none)
263 | #endif
264 | /* must be the last non short and non temporary opcode */
265 | DEF( nop, 1, 0, 0, none)
266 |
267 | /* temporary opcodes: never emitted in the final bytecode */
268 |
269 | def(set_arg_valid_upto, 3, 0, 0, arg) /* emitted in phase 1, removed in phase 2 */
270 |
271 | def( enter_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */
272 | def( leave_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */
273 |
274 | def( label, 5, 0, 0, label) /* emitted in phase 1, removed in phase 3 */
275 |
276 | def(scope_get_var_undef, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2 */
277 | def( scope_get_var, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2 */
278 | def( scope_put_var, 7, 1, 0, atom_u16) /* emitted in phase 1, removed in phase 2 */
279 | def(scope_delete_var, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2 */
280 | def( scope_make_ref, 11, 0, 2, atom_label_u16) /* emitted in phase 1, removed in phase 2 */
281 | def( scope_get_ref, 7, 0, 2, atom_u16) /* emitted in phase 1, removed in phase 2 */
282 | def(scope_put_var_init, 7, 0, 2, atom_u16) /* emitted in phase 1, removed in phase 2 */
283 | def(scope_get_private_field, 7, 1, 1, atom_u16) /* obj -> value, emitted in phase 1, removed in phase 2 */
284 | def(scope_get_private_field2, 7, 1, 2, atom_u16) /* obj -> obj value, emitted in phase 1, removed in phase 2 */
285 | def(scope_put_private_field, 7, 1, 1, atom_u16) /* obj value ->, emitted in phase 1, removed in phase 2 */
286 |
287 | def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */
288 |
289 | def( line_num, 5, 0, 0, u32) /* emitted in phase 1, removed in phase 3 */
290 |
291 | #if SHORT_OPCODES
292 | DEF( push_minus1, 1, 0, 1, none_int)
293 | DEF( push_0, 1, 0, 1, none_int)
294 | DEF( push_1, 1, 0, 1, none_int)
295 | DEF( push_2, 1, 0, 1, none_int)
296 | DEF( push_3, 1, 0, 1, none_int)
297 | DEF( push_4, 1, 0, 1, none_int)
298 | DEF( push_5, 1, 0, 1, none_int)
299 | DEF( push_6, 1, 0, 1, none_int)
300 | DEF( push_7, 1, 0, 1, none_int)
301 | DEF( push_i8, 2, 0, 1, i8)
302 | DEF( push_i16, 3, 0, 1, i16)
303 | DEF( push_const8, 2, 0, 1, const8)
304 | DEF( fclosure8, 2, 0, 1, const8) /* must follow push_const8 */
305 | DEF(push_empty_string, 1, 0, 1, none)
306 |
307 | DEF( get_loc8, 2, 0, 1, loc8)
308 | DEF( put_loc8, 2, 1, 0, loc8)
309 | DEF( set_loc8, 2, 1, 1, loc8)
310 |
311 | DEF( get_loc0, 1, 0, 1, none_loc)
312 | DEF( get_loc1, 1, 0, 1, none_loc)
313 | DEF( get_loc2, 1, 0, 1, none_loc)
314 | DEF( get_loc3, 1, 0, 1, none_loc)
315 | DEF( put_loc0, 1, 1, 0, none_loc)
316 | DEF( put_loc1, 1, 1, 0, none_loc)
317 | DEF( put_loc2, 1, 1, 0, none_loc)
318 | DEF( put_loc3, 1, 1, 0, none_loc)
319 | DEF( set_loc0, 1, 1, 1, none_loc)
320 | DEF( set_loc1, 1, 1, 1, none_loc)
321 | DEF( set_loc2, 1, 1, 1, none_loc)
322 | DEF( set_loc3, 1, 1, 1, none_loc)
323 | DEF( get_arg0, 1, 0, 1, none_arg)
324 | DEF( get_arg1, 1, 0, 1, none_arg)
325 | DEF( get_arg2, 1, 0, 1, none_arg)
326 | DEF( get_arg3, 1, 0, 1, none_arg)
327 | DEF( put_arg0, 1, 1, 0, none_arg)
328 | DEF( put_arg1, 1, 1, 0, none_arg)
329 | DEF( put_arg2, 1, 1, 0, none_arg)
330 | DEF( put_arg3, 1, 1, 0, none_arg)
331 | DEF( set_arg0, 1, 1, 1, none_arg)
332 | DEF( set_arg1, 1, 1, 1, none_arg)
333 | DEF( set_arg2, 1, 1, 1, none_arg)
334 | DEF( set_arg3, 1, 1, 1, none_arg)
335 | DEF( get_var_ref0, 1, 0, 1, none_var_ref)
336 | DEF( get_var_ref1, 1, 0, 1, none_var_ref)
337 | DEF( get_var_ref2, 1, 0, 1, none_var_ref)
338 | DEF( get_var_ref3, 1, 0, 1, none_var_ref)
339 | DEF( put_var_ref0, 1, 1, 0, none_var_ref)
340 | DEF( put_var_ref1, 1, 1, 0, none_var_ref)
341 | DEF( put_var_ref2, 1, 1, 0, none_var_ref)
342 | DEF( put_var_ref3, 1, 1, 0, none_var_ref)
343 | DEF( set_var_ref0, 1, 1, 1, none_var_ref)
344 | DEF( set_var_ref1, 1, 1, 1, none_var_ref)
345 | DEF( set_var_ref2, 1, 1, 1, none_var_ref)
346 | DEF( set_var_ref3, 1, 1, 1, none_var_ref)
347 |
348 | DEF( get_length, 1, 1, 1, none)
349 |
350 | DEF( if_false8, 2, 1, 0, label8)
351 | DEF( if_true8, 2, 1, 0, label8) /* must come after if_false8 */
352 | DEF( goto8, 2, 0, 0, label8) /* must come after if_true8 */
353 | DEF( goto16, 3, 0, 0, label16)
354 |
355 | DEF( call0, 1, 1, 1, npopx)
356 | DEF( call1, 1, 1, 1, npopx)
357 | DEF( call2, 1, 1, 1, npopx)
358 | DEF( call3, 1, 1, 1, npopx)
359 |
360 | DEF( is_undefined, 1, 1, 1, none)
361 | DEF( is_null, 1, 1, 1, none)
362 | DEF( is_function, 1, 1, 1, none)
363 | #endif
364 |
365 | #undef DEF
366 | #undef def
367 | #endif /* DEF */
368 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/readme.txt:
--------------------------------------------------------------------------------
1 | The main documentation is in doc/quickjs.pdf or doc/quickjs.html.
2 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/release.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # Release the QuickJS source code
3 |
4 | set -e
5 |
6 | version=`cat VERSION`
7 |
8 | if [ "$1" = "-h" ] ; then
9 | echo "release.sh [all]"
10 | echo ""
11 | echo "all: build all the archives. Otherwise only build the quickjs source archive."
12 | exit 1
13 | fi
14 |
15 | extras="no"
16 | binary="no"
17 | quickjs="no"
18 |
19 | if [ "$1" = "all" ] ; then
20 | extras="yes"
21 | binary="yes"
22 | quickjs="yes"
23 | elif [ "$1" = "binary" ] ; then
24 | binary="yes"
25 | else
26 | quickjs="yes"
27 | fi
28 |
29 | #################################################"
30 | # extras
31 |
32 | if [ "$extras" = "yes" ] ; then
33 |
34 | d="quickjs-${version}"
35 | name="quickjs-extras-${version}"
36 | outdir="/tmp/${d}"
37 |
38 | rm -rf $outdir
39 | mkdir -p $outdir $outdir/unicode $outdir/tests
40 |
41 | cp unicode/* $outdir/unicode
42 | cp -a tests/bench-v8 $outdir/tests
43 |
44 | ( cd /tmp && tar Jcvf /tmp/${name}.tar.xz ${d} )
45 |
46 | fi
47 |
48 | #################################################"
49 | # binary release
50 |
51 | if [ "$binary" = "yes" ] ; then
52 |
53 | make -j4 qjs run-test262
54 | make -j4 CONFIG_M32=y qjs32 run-test262-32
55 | strip qjs run-test262 qjs32 run-test262-32
56 |
57 | d="quickjs-linux-x86_64-${version}"
58 | outdir="/tmp/${d}"
59 |
60 | rm -rf $outdir
61 | mkdir -p $outdir
62 |
63 | cp qjs run-test262 $outdir
64 |
65 | ( cd /tmp/$d && rm -f ../${d}.zip && zip -r ../${d}.zip . )
66 |
67 | d="quickjs-linux-i686-${version}"
68 | outdir="/tmp/${d}"
69 |
70 | rm -rf $outdir
71 | mkdir -p $outdir
72 |
73 | cp qjs32 $outdir/qjs
74 | cp run-test262-32 $outdir/run-test262
75 |
76 | ( cd /tmp/$d && rm -f ../${d}.zip && zip -r ../${d}.zip . )
77 |
78 | fi
79 |
80 | #################################################"
81 | # quickjs
82 |
83 | if [ "$quickjs" = "yes" ] ; then
84 |
85 | make build_doc
86 |
87 | d="quickjs-${version}"
88 | outdir="/tmp/${d}"
89 |
90 | rm -rf $outdir
91 | mkdir -p $outdir $outdir/doc $outdir/tests $outdir/examples
92 |
93 | cp Makefile VERSION TODO Changelog readme.txt release.sh unicode_download.sh \
94 | qjs.c qjsc.c qjscalc.js repl.js \
95 | quickjs.c quickjs.h quickjs-atom.h \
96 | quickjs-libc.c quickjs-libc.h quickjs-opcode.h \
97 | cutils.c cutils.h list.h \
98 | libregexp.c libregexp.h libregexp-opcode.h \
99 | libunicode.c libunicode.h libunicode-table.h \
100 | libbf.c libbf.h \
101 | jscompress.c unicode_gen.c unicode_gen_def.h \
102 | run-test262.c test262o.conf test262.conf \
103 | test262o_errors.txt test262_errors.txt \
104 | $outdir
105 |
106 | cp tests/*.js tests/*.patch tests/bjson.c $outdir/tests
107 |
108 | cp examples/*.js examples/*.c $outdir/examples
109 |
110 | cp doc/quickjs.texi doc/quickjs.pdf doc/quickjs.html \
111 | doc/jsbignum.texi doc/jsbignum.html doc/jsbignum.pdf \
112 | $outdir/doc
113 |
114 | ( cd /tmp && tar Jcvf /tmp/${d}.tar.xz ${d} )
115 |
116 | fi
117 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/test262.conf:
--------------------------------------------------------------------------------
1 | [config]
2 | # general settings for test262 ES6 version
3 |
4 | # framework style: old, new
5 | style=new
6 |
7 | # handle tests tagged as [noStrict]: yes, no, skip
8 | nostrict=yes
9 |
10 | # handle tests tagged as [strictOnly]: yes, no, skip
11 | strict=yes
12 |
13 | # test mode: default, default-nostrict, default-strict, strict, nostrict, both, all
14 | mode=default
15 |
16 | # handle tests flagged as [async]: yes, no, skip
17 | # for these, load 'harness/doneprintHandle.js' prior to test
18 | # and expect `print('Test262:AsyncTestComplete')` to be called for
19 | # successful termination
20 | async=yes
21 |
22 | # handle tests flagged as [module]: yes, no, skip
23 | module=yes
24 |
25 | # output error messages: yes, no
26 | verbose=yes
27 |
28 | # load harness files from this directory
29 | harnessdir=test262/harness
30 |
31 | # names of harness include files to skip
32 | #harnessexclude=
33 |
34 | # name of the error file for known errors
35 | errorfile=test262_errors.txt
36 |
37 | # exclude tests enumerated in this file (see also [exclude] section)
38 | #excludefile=test262_exclude.txt
39 |
40 | # report test results to this file
41 | reportfile=test262_report.txt
42 |
43 | # enumerate tests from this directory
44 | testdir=test262/test
45 |
46 | [features]
47 | # Standard language features and proposed extensions
48 | # list the features that are included
49 | # skipped features are tagged as such to avoid warnings
50 |
51 | AggregateError=skip
52 | Array.prototype.flat
53 | Array.prototype.flatMap
54 | Array.prototype.flatten
55 | Array.prototype.values
56 | ArrayBuffer
57 | arrow-function
58 | async-functions
59 | async-iteration
60 | Atomics
61 | BigInt
62 | caller
63 | class
64 | class-fields-private
65 | class-fields-public
66 | class-methods-private
67 | class-static-fields-public
68 | class-static-fields-private
69 | class-static-methods-private
70 | coalesce-expression
71 | computed-property-names
72 | const
73 | cross-realm=skip
74 | DataView
75 | DataView.prototype.getFloat32
76 | DataView.prototype.getFloat64
77 | DataView.prototype.getInt16
78 | DataView.prototype.getInt32
79 | DataView.prototype.getInt8
80 | DataView.prototype.getUint16
81 | DataView.prototype.getUint32
82 | DataView.prototype.setUint8
83 | default-arg
84 | default-parameters
85 | destructuring-assignment
86 | destructuring-binding
87 | dynamic-import
88 | export-star-as-namespace-from-module
89 | FinalizationGroup=skip
90 | FinalizationRegistry=skip
91 | Float32Array
92 | Float64Array
93 | for-in-order
94 | for-of
95 | generators
96 | globalThis
97 | hashbang
98 | host-gc-required=skip
99 | import.meta
100 | Int32Array
101 | Int8Array
102 | IsHTMLDDA=skip
103 | json-superset
104 | let
105 | Map
106 | new.target
107 | numeric-separator-literal
108 | object-rest
109 | object-spread
110 | Object.fromEntries
111 | Object.is
112 | optional-catch-binding
113 | optional-chaining
114 | Promise.allSettled
115 | Promise.prototype.finally
116 | Proxy
117 | proxy-missing-checks
118 | Reflect
119 | Reflect.construct
120 | Reflect.set
121 | Reflect.setPrototypeOf
122 | regexp-dotall
123 | regexp-lookbehind
124 | regexp-match-indices=skip
125 | regexp-named-groups
126 | regexp-unicode-property-escapes
127 | rest-parameters
128 | Set
129 | SharedArrayBuffer
130 | string-trimming
131 | String.fromCodePoint
132 | String.prototype.endsWith
133 | String.prototype.includes
134 | String.prototype.matchAll
135 | String.prototype.replaceAll
136 | String.prototype.trimEnd
137 | String.prototype.trimStart
138 | super
139 | Symbol
140 | Symbol.asyncIterator
141 | Symbol.hasInstance
142 | Symbol.isConcatSpreadable
143 | Symbol.iterator
144 | Symbol.match
145 | Symbol.matchAll
146 | Symbol.prototype.description
147 | Symbol.replace
148 | Symbol.search
149 | Symbol.species
150 | Symbol.split
151 | Symbol.toPrimitive
152 | Symbol.toStringTag
153 | Symbol.unscopables
154 | tail-call-optimization=skip
155 | template
156 | top-level-await=skip
157 | TypedArray
158 | u180e
159 | Uint16Array
160 | Uint8Array
161 | Uint8ClampedArray
162 | WeakMap
163 | WeakRef=skip
164 | WeakSet
165 | well-formed-json-stringify
166 |
167 | [exclude]
168 | # list excluded tests and directories here
169 |
170 | # intl not supported
171 | test262/test/intl402/
172 |
173 | # incompatible with the "caller" feature
174 | test262/test/built-ins/Function/prototype/restricted-property-caller.js
175 | test262/test/built-ins/Function/prototype/restricted-property-arguments.js
176 | test262/test/built-ins/ThrowTypeError/unique-per-realm-function-proto.js
177 |
178 | # slow tests
179 | #test262/test/built-ins/RegExp/CharacterClassEscapes/
180 | #test262/test/built-ins/RegExp/property-escapes/
181 |
182 | [tests]
183 | # list test files or use config.testdir
184 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/test262_errors.txt:
--------------------------------------------------------------------------------
1 | test262/test/language/expressions/arrow-function/eval-var-scope-syntax-err.js:47: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
2 | test262/test/language/expressions/async-arrow-function/eval-var-scope-syntax-err.js:49: TypeError: $DONE() not called
3 | test262/test/language/expressions/async-function/named-eval-var-scope-syntax-err.js:33: TypeError: $DONE() not called
4 | test262/test/language/expressions/async-function/nameless-eval-var-scope-syntax-err.js:33: TypeError: $DONE() not called
5 | test262/test/language/expressions/async-generator/eval-var-scope-syntax-err.js:28: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
6 | test262/test/language/expressions/async-generator/named-eval-var-scope-syntax-err.js:28: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
7 | test262/test/language/expressions/dynamic-import/usage-from-eval.js:26: TypeError: $DONE() not called
8 | test262/test/language/expressions/dynamic-import/usage-from-eval.js:26: strict mode: TypeError: $DONE() not called
9 | test262/test/language/expressions/function/eval-var-scope-syntax-err.js:48: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
10 | test262/test/language/expressions/generators/eval-var-scope-syntax-err.js:49: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
11 | test262/test/language/expressions/object/method-definition/async-gen-meth-eval-var-scope-syntax-err.js:32: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
12 | test262/test/language/expressions/object/method-definition/async-meth-eval-var-scope-syntax-err.js:36: TypeError: $DONE() not called
13 | test262/test/language/expressions/object/method-definition/gen-meth-eval-var-scope-syntax-err.js:54: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
14 | test262/test/language/expressions/object/method-definition/meth-eval-var-scope-syntax-err.js:50: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
15 | test262/test/language/expressions/optional-chaining/optional-call-preserves-this.js:21: TypeError: value has no property
16 | test262/test/language/expressions/optional-chaining/optional-call-preserves-this.js:15: strict mode: TypeError: value has no property
17 | test262/test/language/statements/async-function/eval-var-scope-syntax-err.js:33: TypeError: $DONE() not called
18 | test262/test/language/statements/async-generator/eval-var-scope-syntax-err.js:28: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
19 | test262/test/language/statements/for-await-of/async-gen-decl-dstr-array-elem-iter-rtrn-close-null.js:81: TypeError: $DONE() not called
20 | test262/test/language/statements/for-await-of/async-gen-decl-dstr-array-elem-iter-rtrn-close-null.js:81: strict mode: TypeError: $DONE() not called
21 | test262/test/language/statements/function/eval-var-scope-syntax-err.js:49: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
22 | test262/test/language/statements/generators/eval-var-scope-syntax-err.js:49: Test262Error: Expected a SyntaxError to be thrown but no exception was thrown at all
23 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/test262o_errors.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1160007652/safe-wasm-jsvm/b03e2f28c062e902d3c8d053eca7662fe9d0ad3d/lib/quickjs-2020-03-16/test262o_errors.txt
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/bjson.c:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS: binary JSON module (test only)
3 | *
4 | * Copyright (c) 2017-2019 Fabrice Bellard
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | #include "../quickjs-libc.h"
25 | #include "../cutils.h"
26 |
27 | static JSValue js_bjson_read(JSContext *ctx, JSValueConst this_val,
28 | int argc, JSValueConst *argv)
29 | {
30 | uint8_t *buf;
31 | uint64_t pos, len;
32 | JSValue obj;
33 | size_t size;
34 |
35 | if (JS_ToIndex(ctx, &pos, argv[1]))
36 | return JS_EXCEPTION;
37 | if (JS_ToIndex(ctx, &len, argv[2]))
38 | return JS_EXCEPTION;
39 | buf = JS_GetArrayBuffer(ctx, &size, argv[0]);
40 | if (!buf)
41 | return JS_EXCEPTION;
42 | if (pos + len > size)
43 | return JS_ThrowRangeError(ctx, "array buffer overflow");
44 | obj = JS_ReadObject(ctx, buf + pos, len, 0);
45 | return obj;
46 | }
47 |
48 | static JSValue js_bjson_write(JSContext *ctx, JSValueConst this_val,
49 | int argc, JSValueConst *argv)
50 | {
51 | size_t len;
52 | uint8_t *buf;
53 | JSValue array;
54 |
55 | buf = JS_WriteObject(ctx, &len, argv[0], 0);
56 | if (!buf)
57 | return JS_EXCEPTION;
58 | array = JS_NewArrayBufferCopy(ctx, buf, len);
59 | js_free(ctx, buf);
60 | return array;
61 | }
62 |
63 | static const JSCFunctionListEntry js_bjson_funcs[] = {
64 | JS_CFUNC_DEF("read", 3, js_bjson_read ),
65 | JS_CFUNC_DEF("write", 1, js_bjson_write ),
66 | };
67 |
68 | static int js_bjson_init(JSContext *ctx, JSModuleDef *m)
69 | {
70 | return JS_SetModuleExportList(ctx, m, js_bjson_funcs,
71 | countof(js_bjson_funcs));
72 | }
73 |
74 | #ifdef JS_SHARED_LIBRARY
75 | #define JS_INIT_MODULE js_init_module
76 | #else
77 | #define JS_INIT_MODULE js_init_module_bjson
78 | #endif
79 |
80 | JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name)
81 | {
82 | JSModuleDef *m;
83 | m = JS_NewCModule(ctx, module_name, js_bjson_init);
84 | if (!m)
85 | return NULL;
86 | JS_AddModuleExportList(ctx, m, js_bjson_funcs, countof(js_bjson_funcs));
87 | return m;
88 | }
89 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test262.patch:
--------------------------------------------------------------------------------
1 | diff --git a/harness/atomicsHelper.js b/harness/atomicsHelper.js
2 | index 9c1217351e..3c24755558 100644
3 | --- a/harness/atomicsHelper.js
4 | +++ b/harness/atomicsHelper.js
5 | @@ -227,10 +227,14 @@ $262.agent.waitUntil = function(typedArray, index, expected) {
6 | * }
7 | */
8 | $262.agent.timeouts = {
9 | - yield: 100,
10 | - small: 200,
11 | - long: 1000,
12 | - huge: 10000,
13 | +// yield: 100,
14 | +// small: 200,
15 | +// long: 1000,
16 | +// huge: 10000,
17 | + yield: 20,
18 | + small: 20,
19 | + long: 100,
20 | + huge: 1000,
21 | };
22 |
23 | /**
24 | diff --git a/harness/regExpUtils.js b/harness/regExpUtils.js
25 | index be7039fda0..7b38abf8df 100644
26 | --- a/harness/regExpUtils.js
27 | +++ b/harness/regExpUtils.js
28 | @@ -6,24 +6,27 @@ description: |
29 | defines: [buildString, testPropertyEscapes, matchValidator]
30 | ---*/
31 |
32 | +if ($262 && typeof $262.codePointRange === "function") {
33 | + /* use C function to build the codePointRange (much faster with
34 | + slow JS engines) */
35 | + codePointRange = $262.codePointRange;
36 | +} else {
37 | + codePointRange = function codePointRange(start, end) {
38 | + const codePoints = [];
39 | + let length = 0;
40 | + for (codePoint = start; codePoint < end; codePoint++) {
41 | + codePoints[length++] = codePoint;
42 | + }
43 | + return String.fromCodePoint.apply(null, codePoints);
44 | + }
45 | +}
46 | +
47 | function buildString({ loneCodePoints, ranges }) {
48 | - const CHUNK_SIZE = 10000;
49 | - let result = Reflect.apply(String.fromCodePoint, null, loneCodePoints);
50 | - for (let i = 0; i < ranges.length; i++) {
51 | - const range = ranges[i];
52 | - const start = range[0];
53 | - const end = range[1];
54 | - const codePoints = [];
55 | - for (let length = 0, codePoint = start; codePoint <= end; codePoint++) {
56 | - codePoints[length++] = codePoint;
57 | - if (length === CHUNK_SIZE) {
58 | - result += Reflect.apply(String.fromCodePoint, null, codePoints);
59 | - codePoints.length = length = 0;
60 | - }
61 | + let result = String.fromCodePoint.apply(null, loneCodePoints);
62 | + for (const [start, end] of ranges) {
63 | + result += codePointRange(start, end + 1);
64 | }
65 | - result += Reflect.apply(String.fromCodePoint, null, codePoints);
66 | - }
67 | - return result;
68 | + return result;
69 | }
70 |
71 | function testPropertyEscapes(regex, string, expression) {
72 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_bignum.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | function assert(actual, expected, message) {
4 | if (arguments.length == 1)
5 | expected = true;
6 |
7 | if (actual === expected)
8 | return;
9 |
10 | if (actual !== null && expected !== null
11 | && typeof actual == 'object' && typeof expected == 'object'
12 | && actual.toString() === expected.toString())
13 | return;
14 |
15 | throw Error("assertion failed: got |" + actual + "|" +
16 | ", expected |" + expected + "|" +
17 | (message ? " (" + message + ")" : ""));
18 | }
19 |
20 | function assertThrows(err, func)
21 | {
22 | var ex;
23 | ex = false;
24 | try {
25 | func();
26 | } catch(e) {
27 | ex = true;
28 | assert(e instanceof err);
29 | }
30 | assert(ex, true, "exception expected");
31 | }
32 |
33 | // load more elaborate version of assert if available
34 | try { __loadScript("test_assert.js"); } catch(e) {}
35 |
36 | /*----------------*/
37 |
38 | function bigint_pow(a, n)
39 | {
40 | var r, i;
41 | r = 1n;
42 | for(i = 0n; i < n; i++)
43 | r *= a;
44 | return r;
45 | }
46 |
47 | /* a must be < b */
48 | function test_less(a, b)
49 | {
50 | assert(a < b);
51 | assert(!(b < a));
52 | assert(a <= b);
53 | assert(!(b <= a));
54 | assert(b > a);
55 | assert(!(a > b));
56 | assert(b >= a);
57 | assert(!(a >= b));
58 | assert(a != b);
59 | assert(!(a == b));
60 | }
61 |
62 | /* a must be numerically equal to b */
63 | function test_eq(a, b)
64 | {
65 | assert(a == b);
66 | assert(b == a);
67 | assert(!(a != b));
68 | assert(!(b != a));
69 | assert(a <= b);
70 | assert(b <= a);
71 | assert(!(a < b));
72 | assert(a >= b);
73 | assert(b >= a);
74 | assert(!(a > b));
75 | }
76 |
77 | function test_bigint1()
78 | {
79 | var a, r;
80 |
81 | test_less(2n, 3n);
82 | test_eq(3n, 3n);
83 |
84 | test_less(2, 3n);
85 | test_eq(3, 3n);
86 |
87 | test_less(2.1, 3n);
88 | test_eq(Math.sqrt(4), 2n);
89 |
90 | a = bigint_pow(3n, 100n);
91 | assert((a - 1n) != a);
92 | assert(a == 515377520732011331036461129765621272702107522001n);
93 | assert(a == 0x5a4653ca673768565b41f775d6947d55cf3813d1n);
94 |
95 | r = 1n << 31n;
96 | assert(r, 2147483648n, "1 << 31n === 2147483648n");
97 |
98 | r = 1n << 32n;
99 | assert(r, 4294967296n, "1 << 32n === 4294967296n");
100 | }
101 |
102 | function test_bigint2()
103 | {
104 | assert(BigInt(""), 0n);
105 | assert(BigInt(" 123"), 123n);
106 | assert(BigInt(" 123 "), 123n);
107 | assertThrows(SyntaxError, () => { BigInt("+") } );
108 | assertThrows(SyntaxError, () => { BigInt("-") } );
109 | assertThrows(SyntaxError, () => { BigInt("\x00a") } );
110 | assertThrows(SyntaxError, () => { BigInt(" 123 r") } );
111 | }
112 |
113 | function test_divrem(div1, a, b, q)
114 | {
115 | var div, divrem, t;
116 | div = BigInt[div1];
117 | divrem = BigInt[div1 + "rem"];
118 | assert(div(a, b) == q);
119 | t = divrem(a, b);
120 | assert(t[0] == q);
121 | assert(a == b * q + t[1]);
122 | }
123 |
124 | function test_idiv1(div, a, b, r)
125 | {
126 | test_divrem(div, a, b, r[0]);
127 | test_divrem(div, -a, b, r[1]);
128 | test_divrem(div, a, -b, r[2]);
129 | test_divrem(div, -a, -b, r[3]);
130 | }
131 |
132 | /* QuickJS BigInt extensions */
133 | function test_bigint_ext()
134 | {
135 | var r;
136 | assert(BigInt.floorLog2(0n) === -1n);
137 | assert(BigInt.floorLog2(7n) === 2n);
138 |
139 | assert(BigInt.sqrt(0xffffffc000000000000000n) === 17592185913343n);
140 | r = BigInt.sqrtrem(0xffffffc000000000000000n);
141 | assert(r[0] === 17592185913343n);
142 | assert(r[1] === 35167191957503n);
143 |
144 | test_idiv1("tdiv", 3n, 2n, [1n, -1n, -1n, 1n]);
145 | test_idiv1("fdiv", 3n, 2n, [1n, -2n, -2n, 1n]);
146 | test_idiv1("cdiv", 3n, 2n, [2n, -1n, -1n, 2n]);
147 | test_idiv1("ediv", 3n, 2n, [1n, -2n, -1n, 2n]);
148 | }
149 |
150 | function test_bigfloat()
151 | {
152 | var e, a, b, sqrt2;
153 |
154 | assert(typeof 1n === "bigint");
155 | assert(typeof 1l === "bigfloat");
156 | assert(1 == 1.0l);
157 | assert(1 !== 1.0l);
158 |
159 | test_less(2l, 3l);
160 | test_eq(3l, 3l);
161 |
162 | test_less(2, 3l);
163 | test_eq(3, 3l);
164 |
165 | test_less(2.1, 3l);
166 | test_eq(Math.sqrt(9), 3l);
167 |
168 | test_less(2n, 3l);
169 | test_eq(3n, 3l);
170 |
171 | e = new BigFloatEnv(128);
172 | assert(e.prec == 128);
173 | a = BigFloat.sqrt(2l, e);
174 | assert(a === BigFloat.parseFloat("0x1.6a09e667f3bcc908b2fb1366ea957d3e", 0, e));
175 | assert(e.inexact === true);
176 | assert(BigFloat.fpRound(a) == 0x1.6a09e667f3bcc908b2fb1366ea95l);
177 |
178 | b = BigFloatEnv.setPrec(BigFloat.sqrt.bind(null, 2), 128);
179 | assert(a === b);
180 |
181 | assert(BigFloat.isNaN(BigFloat(NaN)));
182 | assert(BigFloat.isFinite(1l));
183 | assert(!BigFloat.isFinite(1l/0l));
184 |
185 | assert(BigFloat.abs(-3l) === 3l);
186 | assert(BigFloat.sign(-3l) === -1l);
187 |
188 | assert(BigFloat.exp(0.2l) === 1.2214027581601698339210719946396742l);
189 | assert(BigFloat.log(3l) === 1.0986122886681096913952452369225256l);
190 | assert(BigFloat.pow(2.1l, 1.6l) === 3.277561666451861947162828744873745l);
191 |
192 | assert(BigFloat.sin(-1l) === -0.841470984807896506652502321630299l);
193 | assert(BigFloat.cos(1l) === 0.5403023058681397174009366074429766l);
194 | assert(BigFloat.tan(0.1l) === 0.10033467208545054505808004578111154l);
195 |
196 | assert(BigFloat.asin(0.3l) === 0.30469265401539750797200296122752915l);
197 | assert(BigFloat.acos(0.4l) === 1.1592794807274085998465837940224159l);
198 | assert(BigFloat.atan(0.7l) === 0.610725964389208616543758876490236l);
199 | assert(BigFloat.atan2(7.1l, -5.1l) === 2.1937053809751415549388104628759813l);
200 |
201 | assert(BigFloat.floor(2.5l) === 2l);
202 | assert(BigFloat.ceil(2.5l) === 3l);
203 | assert(BigFloat.trunc(-2.5l) === -2l);
204 | assert(BigFloat.round(2.5l) === 3l);
205 |
206 | assert(BigFloat.fmod(3l,2l) === 1l);
207 | assert(BigFloat.remainder(3l,2l) === -1l);
208 |
209 | /* string conversion */
210 | assert((1234.125l).toString(), "1234.125");
211 | assert((1234.125l).toFixed(2), "1234.13");
212 | assert((1234.125l).toFixed(2, "down"), "1234.12");
213 | assert((1234.125l).toExponential(), "1.234125e+3");
214 | assert((1234.125l).toExponential(5), "1.23413e+3");
215 | assert((1234.125l).toExponential(5, BigFloatEnv.RNDZ), "1.23412e+3");
216 | assert((1234.125l).toPrecision(6), "1234.13");
217 | assert((1234.125l).toPrecision(6, BigFloatEnv.RNDZ), "1234.12");
218 |
219 | /* string conversion with binary base */
220 | assert((0x123.438l).toString(16), "123.438");
221 | assert((0x323.438l).toString(16), "323.438");
222 | assert((0x723.438l).toString(16), "723.438");
223 | assert((0xf23.438l).toString(16), "f23.438");
224 | assert((0x123.438l).toFixed(2, BigFloatEnv.RNDNA, 16), "123.44");
225 | assert((0x323.438l).toFixed(2, BigFloatEnv.RNDNA, 16), "323.44");
226 | assert((0x723.438l).toFixed(2, BigFloatEnv.RNDNA, 16), "723.44");
227 | assert((0xf23.438l).toFixed(2, BigFloatEnv.RNDNA, 16), "f23.44");
228 | assert((0x0.0000438l).toFixed(6, BigFloatEnv.RNDNA, 16), "0.000044");
229 | assert((0x1230000000l).toFixed(1, BigFloatEnv.RNDNA, 16), "1230000000.0");
230 | assert((0x123.438l).toPrecision(5, BigFloatEnv.RNDNA, 16), "123.44");
231 | assert((0x123.438l).toPrecision(5, BigFloatEnv.RNDZ, 16), "123.43");
232 | assert((0x323.438l).toPrecision(5, BigFloatEnv.RNDNA, 16), "323.44");
233 | assert((0x723.438l).toPrecision(5, BigFloatEnv.RNDNA, 16), "723.44");
234 | assert((-0xf23.438l).toPrecision(5, BigFloatEnv.RNDD, 16), "-f23.44");
235 | assert((0x123.438l).toExponential(4, BigFloatEnv.RNDNA, 16), "1.2344p+8");
236 | }
237 |
238 | function test_bigdecimal()
239 | {
240 | assert(1m === 1m);
241 | assert(1m !== 2m);
242 | test_less(1m, 2m);
243 | test_eq(2m, 2m);
244 |
245 | test_less(1, 2m);
246 | test_eq(2, 2m);
247 |
248 | test_less(1.1, 2m);
249 | test_eq(Math.sqrt(4), 2m);
250 |
251 | test_less(2n, 3m);
252 | test_eq(3n, 3m);
253 |
254 | assert(BigDecimal("1234.1") === 1234.1m);
255 | assert(BigDecimal(" 1234.1") === 1234.1m);
256 | assert(BigDecimal(" 1234.1 ") === 1234.1m);
257 |
258 | assert(BigDecimal(0.1) === 0.1m);
259 | assert(BigDecimal(123) === 123m);
260 | assert(BigDecimal(true) === 1m);
261 |
262 | assert(123m + 1m === 124m);
263 | assert(123m - 1m === 122m);
264 |
265 | assert(3.2m * 3m === 9.6m);
266 | assert(10m / 2m === 5m);
267 | assertThrows(RangeError, () => { 10m / 3m } );
268 |
269 | assert(10m % 3m === 1m);
270 | assert(-10m % 3m === -1m);
271 |
272 | assert(1234.5m ** 3m === 1881365963.625m);
273 | assertThrows(RangeError, () => { 2m ** 3.1m } );
274 | assertThrows(RangeError, () => { 2m ** -3m } );
275 |
276 | assert(BigDecimal.sqrt(2m,
277 | { roundingMode: "half-even",
278 | maximumSignificantDigits: 4 }) === 1.414m);
279 | assert(BigDecimal.sqrt(101m,
280 | { roundingMode: "half-even",
281 | maximumFractionDigits: 3 }) === 10.050m);
282 | assert(BigDecimal.sqrt(0.002m,
283 | { roundingMode: "half-even",
284 | maximumFractionDigits: 3 }) === 0.045m);
285 |
286 | assert(BigDecimal.round(3.14159m,
287 | { roundingMode: "half-even",
288 | maximumFractionDigits: 3 }) === 3.142m);
289 |
290 | assert(BigDecimal.add(3.14159m, 0.31212m,
291 | { roundingMode: "half-even",
292 | maximumFractionDigits: 2 }) === 3.45m);
293 | assert(BigDecimal.sub(3.14159m, 0.31212m,
294 | { roundingMode: "down",
295 | maximumFractionDigits: 2 }) === 2.82m);
296 | assert(BigDecimal.mul(3.14159m, 0.31212m,
297 | { roundingMode: "half-even",
298 | maximumFractionDigits: 3 }) === 0.981m);
299 | assert(BigDecimal.mod(3.14159m, 0.31211m,
300 | { roundingMode: "half-even",
301 | maximumFractionDigits: 4 }) === 0.0205m);
302 | assert(BigDecimal.div(20m, 3m,
303 | { roundingMode: "half-even",
304 | maximumSignificantDigits: 3 }) === 6.67m);
305 | assert(BigDecimal.div(20m, 3m,
306 | { roundingMode: "half-even",
307 | maximumFractionDigits: 50 }) ===
308 | 6.66666666666666666666666666666666666666666666666667m);
309 |
310 | /* string conversion */
311 | assert((1234.125m).toString(), "1234.125");
312 | assert((1234.125m).toFixed(2), "1234.13");
313 | assert((1234.125m).toFixed(2, "down"), "1234.12");
314 | assert((1234.125m).toExponential(), "1.234125e+3");
315 | assert((1234.125m).toExponential(5), "1.23413e+3");
316 | assert((1234.125m).toExponential(5, "down"), "1.23412e+3");
317 | assert((1234.125m).toPrecision(6), "1234.13");
318 | assert((1234.125m).toPrecision(6, "down"), "1234.12");
319 | assert((-1234.125m).toPrecision(6, "floor"), "-1234.13");
320 | }
321 |
322 | test_bigint1();
323 | test_bigint2();
324 | test_bigint_ext();
325 | test_bigfloat();
326 | test_bigdecimal();
327 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_bjson.js:
--------------------------------------------------------------------------------
1 | import * as bjson from "./bjson.so";
2 |
3 | function assert(b, str)
4 | {
5 | if (b) {
6 | return;
7 | } else {
8 | throw Error("assertion failed: " + str);
9 | }
10 | }
11 |
12 | function toHex(a)
13 | {
14 | var i, s = "", tab, v;
15 | tab = new Uint8Array(a);
16 | for(i = 0; i < tab.length; i++) {
17 | v = tab[i].toString(16);
18 | if (v.length < 2)
19 | v = "0" + v;
20 | if (i !== 0)
21 | s += " ";
22 | s += v;
23 | }
24 | return s;
25 | }
26 |
27 | function toStr(a)
28 | {
29 | var s, i, props, prop;
30 |
31 | switch(typeof(a)) {
32 | case "object":
33 | if (a === null)
34 | return "null";
35 | if (Array.isArray(a)) {
36 | s = "[";
37 | for(i = 0; i < a.length; i++) {
38 | if (i != 0)
39 | s += ",";
40 | s += toStr(a[i]);
41 | }
42 | s += "]";
43 | } else {
44 | props = Object.keys(a);
45 | s = "{";
46 | for(i = 0; i < props.length; i++) {
47 | if (i != 0)
48 | s += ",";
49 | prop = props[i];
50 | s += prop + ":" + toStr(a[prop]);
51 | }
52 | s += "}";
53 | }
54 | return s;
55 | case "undefined":
56 | return "undefined";
57 | case "string":
58 | return a.__quote();
59 | case "number":
60 | case "bigfloat":
61 | if (a == 0 && 1 / a < 0)
62 | return "-0";
63 | else
64 | return a.toString();
65 | break;
66 | default:
67 | return a.toString();
68 | }
69 | }
70 |
71 | function bjson_test(a)
72 | {
73 | var buf, r, a_str, r_str;
74 | a_str = toStr(a);
75 | buf = bjson.write(a);
76 | if (0) {
77 | print(a_str, "->", toHex(buf));
78 | }
79 | r = bjson.read(buf, 0, buf.byteLength);
80 | r_str = toStr(r);
81 | if (a_str != r_str) {
82 | print(a_str);
83 | print(r_str);
84 | assert(false);
85 | }
86 | }
87 |
88 | function bjson_test_all()
89 | {
90 | var obj;
91 |
92 | bjson_test({x:1, y:2, if:3});
93 | bjson_test([1, 2, 3]);
94 | bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
95 | if (typeof BigInt !== "undefined") {
96 | bjson_test([BigInt("1"), -BigInt("0x123456789"),
97 | BigInt("0x123456789abcdef123456789abcdef")]);
98 | }
99 | if (typeof BigFloat !== "undefined") {
100 | BigFloatEnv.setPrec(function () {
101 | bjson_test([BigFloat("0.1"), BigFloat("-1e30"), BigFloat("0"),
102 | BigFloat("-0"), BigFloat("Infinity"), BigFloat("-Infinity"),
103 | 0.0 / BigFloat("0"), BigFloat.MAX_VALUE,
104 | BigFloat.MIN_VALUE]);
105 | }, 113, 15);
106 | }
107 | if (typeof BigDecimal !== "undefined") {
108 | bjson_test([BigDecimal("0"),
109 | BigDecimal("0.8"), BigDecimal("123321312321321e100"),
110 | BigDecimal("-1233213123213214332333223332e100"),
111 | BigDecimal("1.233e-1000")]);
112 | }
113 |
114 | /* tested with a circular reference */
115 | obj = {};
116 | obj.x = obj;
117 | try {
118 | bjson.write(obj);
119 | assert(false);
120 | } catch(e) {
121 | assert(e instanceof TypeError);
122 | }
123 | }
124 |
125 | bjson_test_all();
126 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_closure.js:
--------------------------------------------------------------------------------
1 | function assert(actual, expected, message) {
2 | if (arguments.length == 1)
3 | expected = true;
4 |
5 | if (actual === expected)
6 | return;
7 |
8 | if (actual !== null && expected !== null
9 | && typeof actual == 'object' && typeof expected == 'object'
10 | && actual.toString() === expected.toString())
11 | return;
12 |
13 | throw Error("assertion failed: got |" + actual + "|" +
14 | ", expected |" + expected + "|" +
15 | (message ? " (" + message + ")" : ""));
16 | }
17 |
18 | // load more elaborate version of assert if available
19 | try { __loadScript("test_assert.js"); } catch(e) {}
20 |
21 | /*----------------*/
22 |
23 | var log_str = "";
24 |
25 | function log(str)
26 | {
27 | log_str += str + ",";
28 | }
29 |
30 | function f(a, b, c)
31 | {
32 | var x = 10;
33 | log("a="+a);
34 | function g(d) {
35 | function h() {
36 | log("d=" + d);
37 | log("x=" + x);
38 | }
39 | log("b=" + b);
40 | log("c=" + c);
41 | h();
42 | }
43 | g(4);
44 | return g;
45 | }
46 |
47 | var g1 = f(1, 2, 3);
48 | g1(5);
49 |
50 | assert(log_str, "a=1,b=2,c=3,d=4,x=10,b=2,c=3,d=5,x=10,", "closure1");
51 |
52 | function test_closure1()
53 | {
54 | function f2()
55 | {
56 | var val = 1;
57 |
58 | function set(a) {
59 | val = a;
60 | }
61 | function get(a) {
62 | return val;
63 | }
64 | return { "set": set, "get": get };
65 | }
66 |
67 | var obj = f2();
68 | obj.set(10);
69 | var r;
70 | r = obj.get();
71 | assert(r, 10, "closure2");
72 | }
73 |
74 | function test_closure2()
75 | {
76 | var expr_func = function myfunc1(n) {
77 | function myfunc2(n) {
78 | return myfunc1(n - 1);
79 | }
80 | if (n == 0)
81 | return 0;
82 | else
83 | return myfunc2(n);
84 | };
85 | var r;
86 | r = expr_func(1);
87 | assert(r, 0, "expr_func");
88 | }
89 |
90 | function test_closure3()
91 | {
92 | function fib(n)
93 | {
94 | if (n <= 0)
95 | return 0;
96 | else if (n == 1)
97 | return 1;
98 | else
99 | return fib(n - 1) + fib(n - 2);
100 | }
101 |
102 | var fib_func = function fib1(n)
103 | {
104 | if (n <= 0)
105 | return 0;
106 | else if (n == 1)
107 | return 1;
108 | else
109 | return fib1(n - 1) + fib1(n - 2);
110 | };
111 |
112 | assert(fib(6), 8, "fib");
113 | assert(fib_func(6), 8, "fib_func");
114 | }
115 |
116 | function test_arrow_function()
117 | {
118 | "use strict";
119 |
120 | function f1() {
121 | return (() => arguments)();
122 | }
123 | function f2() {
124 | return (() => this)();
125 | }
126 | function f3() {
127 | return (() => eval("this"))();
128 | }
129 | function f4() {
130 | return (() => eval("new.target"))();
131 | }
132 | var a;
133 |
134 | a = f1(1, 2);
135 | assert(a.length, 2);
136 | assert(a[0] === 1 && a[1] === 2);
137 |
138 | assert(f2.call("this_val") === "this_val");
139 | assert(f3.call("this_val") === "this_val");
140 | assert(new f4() === f4);
141 |
142 | var o1 = { f() { return this; } };
143 | var o2 = { f() {
144 | return (() => eval("super.f()"))();
145 | } };
146 | o2.__proto__ = o1;
147 |
148 | assert(o2.f() === o2);
149 | }
150 |
151 | function test_with()
152 | {
153 | var o1 = { x: "o1", y: "o1" };
154 | var x = "local";
155 | eval('var z="var_obj";');
156 | assert(z === "var_obj");
157 | with (o1) {
158 | assert(x === "o1");
159 | assert(eval("x") === "o1");
160 | var f = function () {
161 | o2 = { x: "o2" };
162 | with (o2) {
163 | assert(x === "o2");
164 | assert(y === "o1");
165 | assert(z === "var_obj");
166 | assert(eval("x") === "o2");
167 | assert(eval("y") === "o1");
168 | assert(eval("z") === "var_obj");
169 | assert(eval('eval("x")') === "o2");
170 | }
171 | };
172 | f();
173 | }
174 | }
175 |
176 | function test_eval_closure()
177 | {
178 | var tab;
179 |
180 | tab = [];
181 | for(let i = 0; i < 3; i++) {
182 | eval("tab.push(function g1() { return i; })");
183 | }
184 | for(let i = 0; i < 3; i++) {
185 | assert(tab[i]() === i);
186 | }
187 |
188 | tab = [];
189 | for(let i = 0; i < 3; i++) {
190 | let f = function f() {
191 | eval("tab.push(function g2() { return i; })");
192 | };
193 | f();
194 | }
195 | for(let i = 0; i < 3; i++) {
196 | assert(tab[i]() === i);
197 | }
198 | }
199 |
200 | function test_eval_const()
201 | {
202 | const a = 1;
203 | var success = false;
204 | var f = function () {
205 | eval("a = 1");
206 | };
207 | try {
208 | f();
209 | } catch(e) {
210 | success = (e instanceof TypeError);
211 | }
212 | assert(success);
213 | }
214 |
215 | test_closure1();
216 | test_closure2();
217 | test_closure3();
218 | test_arrow_function();
219 | test_with();
220 | test_eval_closure();
221 | test_eval_const();
222 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_loop.js:
--------------------------------------------------------------------------------
1 | function assert(actual, expected, message) {
2 | if (arguments.length == 1)
3 | expected = true;
4 |
5 | if (actual === expected)
6 | return;
7 |
8 | if (actual !== null && expected !== null
9 | && typeof actual == 'object' && typeof expected == 'object'
10 | && actual.toString() === expected.toString())
11 | return;
12 |
13 | throw Error("assertion failed: got |" + actual + "|" +
14 | ", expected |" + expected + "|" +
15 | (message ? " (" + message + ")" : ""));
16 | }
17 |
18 | // load more elaborate version of assert if available
19 | try { __loadScript("test_assert.js"); } catch(e) {}
20 |
21 | /*----------------*/
22 |
23 | function test_while()
24 | {
25 | var i, c;
26 | i = 0;
27 | c = 0;
28 | while (i < 3) {
29 | c++;
30 | i++;
31 | }
32 | assert(c === 3);
33 | }
34 |
35 | function test_while_break()
36 | {
37 | var i, c;
38 | i = 0;
39 | c = 0;
40 | while (i < 3) {
41 | c++;
42 | if (i == 1)
43 | break;
44 | i++;
45 | }
46 | assert(c === 2 && i === 1);
47 | }
48 |
49 | function test_do_while()
50 | {
51 | var i, c;
52 | i = 0;
53 | c = 0;
54 | do {
55 | c++;
56 | i++;
57 | } while (i < 3);
58 | assert(c === 3 && i === 3);
59 | }
60 |
61 | function test_for()
62 | {
63 | var i, c;
64 | c = 0;
65 | for(i = 0; i < 3; i++) {
66 | c++;
67 | }
68 | assert(c === 3 && i === 3);
69 |
70 | c = 0;
71 | for(var j = 0; j < 3; j++) {
72 | c++;
73 | }
74 | assert(c === 3 && j === 3);
75 | }
76 |
77 | function test_for_in()
78 | {
79 | var i, tab, a, b;
80 |
81 | tab = [];
82 | for(i in {x:1, y: 2}) {
83 | tab.push(i);
84 | }
85 | assert(tab.toString(), "x,y", "for_in");
86 |
87 | /* prototype chain test */
88 | a = {x:2, y: 2, "1": 3};
89 | b = {"4" : 3 };
90 | Object.setPrototypeOf(a, b);
91 | tab = [];
92 | for(i in a) {
93 | tab.push(i);
94 | }
95 | assert(tab.toString(), "1,x,y,4", "for_in");
96 |
97 | /* non enumerable properties hide enumerables ones in the
98 | prototype chain */
99 | a = {y: 2, "1": 3};
100 | Object.defineProperty(a, "x", { value: 1 });
101 | b = {"x" : 3 };
102 | Object.setPrototypeOf(a, b);
103 | tab = [];
104 | for(i in a) {
105 | tab.push(i);
106 | }
107 | assert(tab.toString(), "1,y", "for_in");
108 |
109 | /* array optimization */
110 | a = [];
111 | for(i = 0; i < 10; i++)
112 | a.push(i);
113 | tab = [];
114 | for(i in a) {
115 | tab.push(i);
116 | }
117 | assert(tab.toString(), "0,1,2,3,4,5,6,7,8,9", "for_in");
118 |
119 | /* iterate with a field */
120 | a={x:0};
121 | tab = [];
122 | for(a.x in {x:1, y: 2}) {
123 | tab.push(a.x);
124 | }
125 | assert(tab.toString(), "x,y", "for_in");
126 |
127 | /* iterate with a variable field */
128 | a=[0];
129 | tab = [];
130 | for(a[0] in {x:1, y: 2}) {
131 | tab.push(a[0]);
132 | }
133 | assert(tab.toString(), "x,y", "for_in");
134 |
135 | /* variable definition in the for in */
136 | tab = [];
137 | for(var j in {x:1, y: 2}) {
138 | tab.push(j);
139 | }
140 | assert(tab.toString(), "x,y", "for_in");
141 |
142 | /* variable assigment in the for in */
143 | tab = [];
144 | for(var k = 2 in {x:1, y: 2}) {
145 | tab.push(k);
146 | }
147 | assert(tab.toString(), "x,y", "for_in");
148 | }
149 |
150 | function test_for_in2()
151 | {
152 | var i;
153 | tab = [];
154 | for(i in {x:1, y: 2, z:3}) {
155 | if (i === "y")
156 | continue;
157 | tab.push(i);
158 | }
159 | assert(tab.toString() == "x,z");
160 |
161 | tab = [];
162 | for(i in {x:1, y: 2, z:3}) {
163 | if (i === "z")
164 | break;
165 | tab.push(i);
166 | }
167 | assert(tab.toString() == "x,y");
168 | }
169 |
170 | function test_for_break()
171 | {
172 | var i, c;
173 | c = 0;
174 | L1: for(i = 0; i < 3; i++) {
175 | c++;
176 | if (i == 0)
177 | continue;
178 | while (1) {
179 | break L1;
180 | }
181 | }
182 | assert(c === 2 && i === 1);
183 | }
184 |
185 | function test_switch1()
186 | {
187 | var i, a, s;
188 | s = "";
189 | for(i = 0; i < 3; i++) {
190 | a = "?";
191 | switch(i) {
192 | case 0:
193 | a = "a";
194 | break;
195 | case 1:
196 | a = "b";
197 | break;
198 | default:
199 | a = "c";
200 | break;
201 | }
202 | s += a;
203 | }
204 | assert(s === "abc" && i === 3);
205 | }
206 |
207 | function test_switch2()
208 | {
209 | var i, a, s;
210 | s = "";
211 | for(i = 0; i < 4; i++) {
212 | a = "?";
213 | switch(i) {
214 | case 0:
215 | a = "a";
216 | break;
217 | case 1:
218 | a = "b";
219 | break;
220 | case 2:
221 | continue;
222 | default:
223 | a = "" + i;
224 | break;
225 | }
226 | s += a;
227 | }
228 | assert(s === "ab3" && i === 4);
229 | }
230 |
231 | function test_try_catch1()
232 | {
233 | try {
234 | throw "hello";
235 | } catch (e) {
236 | assert(e, "hello", "catch");
237 | return;
238 | }
239 | assert(false, "catch");
240 | }
241 |
242 | function test_try_catch2()
243 | {
244 | var a;
245 | try {
246 | a = 1;
247 | } catch (e) {
248 | a = 2;
249 | }
250 | assert(a, 1, "catch");
251 | }
252 |
253 | function test_try_catch3()
254 | {
255 | var s;
256 | s = "";
257 | try {
258 | s += "t";
259 | } catch (e) {
260 | s += "c";
261 | } finally {
262 | s += "f";
263 | }
264 | assert(s, "tf", "catch");
265 | }
266 |
267 | function test_try_catch4()
268 | {
269 | var s;
270 | s = "";
271 | try {
272 | s += "t";
273 | throw "c";
274 | } catch (e) {
275 | s += e;
276 | } finally {
277 | s += "f";
278 | }
279 | assert(s, "tcf", "catch");
280 | }
281 |
282 | function test_try_catch5()
283 | {
284 | var s;
285 | s = "";
286 | for(;;) {
287 | try {
288 | s += "t";
289 | break;
290 | s += "b";
291 | } finally {
292 | s += "f";
293 | }
294 | }
295 | assert(s, "tf", "catch");
296 | }
297 |
298 | function test_try_catch6()
299 | {
300 | function f() {
301 | try {
302 | s += 't';
303 | return 1;
304 | } finally {
305 | s += "f";
306 | }
307 | }
308 | var s = "";
309 | assert(f() === 1);
310 | assert(s, "tf", "catch6");
311 | }
312 |
313 | function test_try_catch7()
314 | {
315 | var s;
316 | s = "";
317 |
318 | try {
319 | try {
320 | s += "t";
321 | throw "a";
322 | } finally {
323 | s += "f";
324 | }
325 | } catch(e) {
326 | s += e;
327 | } finally {
328 | s += "g";
329 | }
330 | assert(s, "tfag", "catch");
331 | }
332 |
333 | function test_try_catch8()
334 | {
335 | var i, s;
336 |
337 | s = "";
338 | for(var i in {x:1, y:2}) {
339 | try {
340 | s += i;
341 | throw "a";
342 | } catch (e) {
343 | s += e;
344 | } finally {
345 | s += "f";
346 | }
347 | }
348 | assert(s === "xafyaf");
349 | }
350 |
351 | test_while();
352 | test_while_break();
353 | test_do_while();
354 | test_for();
355 | test_for_break();
356 | test_switch1();
357 | test_switch2();
358 | test_for_in();
359 | test_for_in2();
360 |
361 | test_try_catch1();
362 | test_try_catch2();
363 | test_try_catch3();
364 | test_try_catch4();
365 | test_try_catch5();
366 | test_try_catch6();
367 | test_try_catch7();
368 | test_try_catch8();
369 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_op.js:
--------------------------------------------------------------------------------
1 | function assert(actual, expected, message) {
2 | if (arguments.length == 1)
3 | expected = true;
4 |
5 | if (actual === expected)
6 | return;
7 |
8 | if (actual !== null && expected !== null
9 | && typeof actual == 'object' && typeof expected == 'object'
10 | && actual.toString() === expected.toString())
11 | return;
12 |
13 | throw Error("assertion failed: got |" + actual + "|" +
14 | ", expected |" + expected + "|" +
15 | (message ? " (" + message + ")" : ""));
16 | }
17 |
18 | // load more elaborate version of assert if available
19 | try { __loadScript("test_assert.js"); } catch(e) {}
20 |
21 | /*----------------*/
22 |
23 | function test_op1()
24 | {
25 | var r, a;
26 | r = 1 + 2;
27 | assert(r, 3, "1 + 2 === 3");
28 |
29 | r = 1 - 2;
30 | assert(r, -1, "1 - 2 === -1");
31 |
32 | r = -1;
33 | assert(r, -1, "-1 === -1");
34 |
35 | r = +2;
36 | assert(r, 2, "+2 === 2");
37 |
38 | r = 2 * 3;
39 | assert(r, 6, "2 * 3 === 6");
40 |
41 | r = 4 / 2;
42 | assert(r, 2, "4 / 2 === 2");
43 |
44 | r = 4 % 3;
45 | assert(r, 1, "4 % 3 === 3");
46 |
47 | r = 4 << 2;
48 | assert(r, 16, "4 << 2 === 16");
49 |
50 | r = 1 << 0;
51 | assert(r, 1, "1 << 0 === 1");
52 |
53 | r = 1 << 31;
54 | assert(r, -2147483648, "1 << 31 === -2147483648");
55 |
56 | r = 1 << 32;
57 | assert(r, 1, "1 << 32 === 1");
58 |
59 | r = (1 << 31) < 0;
60 | assert(r, true, "(1 << 31) < 0 === true");
61 |
62 | r = -4 >> 1;
63 | assert(r, -2, "-4 >> 1 === -2");
64 |
65 | r = -4 >>> 1;
66 | assert(r, 0x7ffffffe, "-4 >>> 1 === 0x7ffffffe");
67 |
68 | r = 1 & 1;
69 | assert(r, 1, "1 & 1 === 1");
70 |
71 | r = 0 | 1;
72 | assert(r, 1, "0 | 1 === 1");
73 |
74 | r = 1 ^ 1;
75 | assert(r, 0, "1 ^ 1 === 0");
76 |
77 | r = ~1;
78 | assert(r, -2, "~1 === -2");
79 |
80 | r = !1;
81 | assert(r, false, "!1 === false");
82 |
83 | assert((1 < 2), true, "(1 < 2) === true");
84 |
85 | assert((2 > 1), true, "(2 > 1) === true");
86 |
87 | assert(('b' > 'a'), true, "('b' > 'a') === true");
88 |
89 | assert(2 ** 8, 256, "2 ** 8 === 256");
90 | }
91 |
92 | function test_cvt()
93 | {
94 | assert((NaN | 0) === 0);
95 | assert((Infinity | 0) === 0);
96 | assert(((-Infinity) | 0) === 0);
97 | assert(("12345" | 0) === 12345);
98 | assert(("0x12345" | 0) === 0x12345);
99 | assert(((4294967296 * 3 - 4) | 0) === -4);
100 |
101 | assert(("12345" >>> 0) === 12345);
102 | assert(("0x12345" >>> 0) === 0x12345);
103 | assert((NaN >>> 0) === 0);
104 | assert((Infinity >>> 0) === 0);
105 | assert(((-Infinity) >>> 0) === 0);
106 | assert(((4294967296 * 3 - 4) >>> 0) === (4294967296 - 4));
107 | }
108 |
109 | function test_eq()
110 | {
111 | assert(null == undefined);
112 | assert(undefined == null);
113 | assert(true == 1);
114 | assert(0 == false);
115 | assert("" == 0);
116 | assert("123" == 123);
117 | assert("122" != 123);
118 | assert((new Number(1)) == 1);
119 | assert(2 == (new Number(2)));
120 | assert((new String("abc")) == "abc");
121 | assert({} != "abc");
122 | }
123 |
124 | function test_inc_dec()
125 | {
126 | var a, r;
127 |
128 | a = 1;
129 | r = a++;
130 | assert(r === 1 && a === 2, true, "++");
131 |
132 | a = 1;
133 | r = ++a;
134 | assert(r === 2 && a === 2, true, "++");
135 |
136 | a = 1;
137 | r = a--;
138 | assert(r === 1 && a === 0, true, "--");
139 |
140 | a = 1;
141 | r = --a;
142 | assert(r === 0 && a === 0, true, "--");
143 |
144 | a = {x:true};
145 | a.x++;
146 | assert(a.x, 2, "++");
147 |
148 | a = {x:true};
149 | a.x--;
150 | assert(a.x, 0, "--");
151 |
152 | a = [true];
153 | a[0]++;
154 | assert(a[0], 2, "++");
155 |
156 | a = {x:true};
157 | r = a.x++;
158 | assert(r === 1 && a.x === 2, true, "++");
159 |
160 | a = {x:true};
161 | r = a.x--;
162 | assert(r === 1 && a.x === 0, true, "--");
163 |
164 | a = [true];
165 | r = a[0]++;
166 | assert(r === 1 && a[0] === 2, true, "++");
167 |
168 | a = [true];
169 | r = a[0]--;
170 | assert(r === 1 && a[0] === 0, true, "--");
171 | }
172 |
173 | function F(x)
174 | {
175 | this.x = x;
176 | }
177 |
178 | function test_op2()
179 | {
180 | var a, b;
181 | a = new Object;
182 | a.x = 1;
183 | assert(a.x, 1, "new");
184 | b = new F(2);
185 | assert(b.x, 2, "new");
186 |
187 | a = {x : 2};
188 | assert(("x" in a), true, "in");
189 | assert(("y" in a), false, "in");
190 |
191 | a = {};
192 | assert((a instanceof Object), true, "instanceof");
193 | assert((a instanceof String), false, "instanceof");
194 |
195 | assert((typeof 1), "number", "typeof");
196 | assert((typeof Object), "function", "typeof");
197 | assert((typeof null), "object", "typeof");
198 | assert((typeof unknown_var), "undefined", "typeof");
199 |
200 | a = {x: 1, if: 2, async: 3};
201 | assert(a.if === 2);
202 | assert(a.async === 3);
203 | }
204 |
205 | function test_delete()
206 | {
207 | var a, err;
208 |
209 | a = {x: 1, y: 1};
210 | assert((delete a.x), true, "delete");
211 | assert(("x" in a), false, "delete");
212 |
213 | /* the following are not tested by test262 */
214 | assert(delete "abc"[100], true);
215 |
216 | err = false;
217 | try {
218 | delete null.a;
219 | } catch(e) {
220 | err = (e instanceof TypeError);
221 | }
222 | assert(err, true, "delete");
223 |
224 | err = false;
225 | try {
226 | a = { f() { delete super.a; } };
227 | a.f();
228 | } catch(e) {
229 | err = (e instanceof ReferenceError);
230 | }
231 | assert(err, true, "delete");
232 | }
233 |
234 | function test_prototype()
235 | {
236 | function f() { }
237 | assert(f.prototype.constructor, f, "prototype");
238 | }
239 |
240 | function test_arguments()
241 | {
242 | function f2() {
243 | assert(arguments.length, 2, "arguments");
244 | assert(arguments[0], 1, "arguments");
245 | assert(arguments[1], 3, "arguments");
246 | }
247 | f2(1, 3);
248 | }
249 |
250 | function test_class()
251 | {
252 | var o;
253 | class C {
254 | constructor() {
255 | this.x = 10;
256 | }
257 | f() {
258 | return 1;
259 | }
260 | static F() {
261 | return -1;
262 | }
263 | get y() {
264 | return 12;
265 | }
266 | };
267 | class D extends C {
268 | constructor() {
269 | super();
270 | this.z = 20;
271 | }
272 | g() {
273 | return 2;
274 | }
275 | static G() {
276 | return -2;
277 | }
278 | h() {
279 | return super.f();
280 | }
281 | static H() {
282 | return super["F"]();
283 | }
284 | }
285 |
286 | assert(C.F() === -1);
287 | assert(Object.getOwnPropertyDescriptor(C.prototype, "y").get.name === "get y");
288 |
289 | o = new C();
290 | assert(o.f() === 1);
291 | assert(o.x === 10);
292 |
293 | assert(D.F() === -1);
294 | assert(D.G() === -2);
295 | assert(D.H() === -1);
296 |
297 | o = new D();
298 | assert(o.f() === 1);
299 | assert(o.g() === 2);
300 | assert(o.x === 10);
301 | assert(o.z === 20);
302 | assert(o.h() === 1);
303 |
304 | /* test class name scope */
305 | var E1 = class E { static F() { return E; } };
306 | assert(E1 === E1.F());
307 | };
308 |
309 | function test_template()
310 | {
311 | var a, b;
312 | b = 123;
313 | a = `abc${b}d`;
314 | assert(a === "abc123d");
315 |
316 | a = String.raw `abc${b}d`;
317 | assert(a === "abc123d");
318 | }
319 |
320 | function test_object_literal()
321 | {
322 | var x = 0, get = 1, set = 2; async = 3;
323 | a = { get: 2, set: 3, async: 4 };
324 | assert(JSON.stringify(a), '{"get":2,"set":3,"async":4}');
325 |
326 | a = { x, get, set, async };
327 | assert(JSON.stringify(a), '{"x":0,"get":1,"set":2,"async":3}');
328 | }
329 |
330 | function test_regexp_skip()
331 | {
332 | var a, b;
333 | [a, b = /abc\(/] = [1];
334 | assert(a === 1);
335 |
336 | [a, b =/abc\(/] = [2];
337 | assert(a === 2);
338 | }
339 |
340 | function test_labels()
341 | {
342 | do x: { break x; } while(0);
343 | if (1)
344 | x: { break x; }
345 | else
346 | x: { break x; }
347 | with ({}) x: { break x; };
348 | while (0) x: { break x; };
349 | }
350 |
351 | test_op1();
352 | test_cvt();
353 | test_eq();
354 | test_inc_dec();
355 | test_op2();
356 | test_delete();
357 | test_prototype();
358 | test_arguments();
359 | test_class();
360 | test_template();
361 | test_object_literal();
362 | test_regexp_skip();
363 | test_labels();
364 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_op_overloading.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | function assert(actual, expected, message) {
4 | if (arguments.length == 1)
5 | expected = true;
6 |
7 | if (actual === expected)
8 | return;
9 |
10 | if (actual !== null && expected !== null
11 | && typeof actual == 'object' && typeof expected == 'object'
12 | && actual.toString() === expected.toString())
13 | return;
14 |
15 | throw Error("assertion failed: got |" + actual + "|" +
16 | ", expected |" + expected + "|" +
17 | (message ? " (" + message + ")" : ""));
18 | }
19 |
20 | /* operators overloading with Operators.create() */
21 | function test_operators_create() {
22 | class Vec2
23 | {
24 | constructor(x, y) {
25 | this.x = x;
26 | this.y = y;
27 | }
28 | static mul_scalar(p1, a) {
29 | var r = new Vec2();
30 | r.x = p1.x * a;
31 | r.y = p1.y * a;
32 | return r;
33 | }
34 | toString() {
35 | return "Vec2(" + this.x + "," + this.y + ")";
36 | }
37 | }
38 |
39 | Vec2.prototype[Symbol.operatorSet] = Operators.create(
40 | {
41 | "+"(p1, p2) {
42 | var r = new Vec2();
43 | r.x = p1.x + p2.x;
44 | r.y = p1.y + p2.y;
45 | return r;
46 | },
47 | "-"(p1, p2) {
48 | var r = new Vec2();
49 | r.x = p1.x - p2.x;
50 | r.y = p1.y - p2.y;
51 | return r;
52 | },
53 | "=="(a, b) {
54 | return a.x == b.x && a.y == b.y;
55 | },
56 | "<"(a, b) {
57 | var r;
58 | /* lexicographic order */
59 | if (a.x == b.x)
60 | r = (a.y < b.y);
61 | else
62 | r = (a.x < b.x);
63 | return r;
64 | },
65 | "++"(a) {
66 | var r = new Vec2();
67 | r.x = a.x + 1;
68 | r.y = a.y + 1;
69 | return r;
70 | }
71 | },
72 | {
73 | left: Number,
74 | "*"(a, b) {
75 | return Vec2.mul_scalar(b, a);
76 | }
77 | },
78 | {
79 | right: Number,
80 | "*"(a, b) {
81 | return Vec2.mul_scalar(a, b);
82 | }
83 | });
84 |
85 | var a = new Vec2(1, 2);
86 | var b = new Vec2(3, 4);
87 | var r;
88 |
89 | r = a * 2 + 3 * b;
90 | assert(r.x === 11 && r.y === 16);
91 | assert(a == a, true);
92 | assert(a == b, false);
93 | assert(a != a, false);
94 | assert(a < b, true);
95 | assert(a <= b, true);
96 | assert(b < a, false);
97 | assert(b <= a, false);
98 | assert(a <= a, true);
99 | assert(a >= a, true);
100 | a++;
101 | assert(a.x === 2 && a.y === 3);
102 | r = ++a;
103 | assert(a.x === 3 && a.y === 4);
104 | assert(r === a);
105 | }
106 |
107 | /* operators overloading thru inheritance */
108 | function test_operators()
109 | {
110 | var Vec2;
111 |
112 | function mul_scalar(p1, a) {
113 | var r = new Vec2();
114 | r.x = p1.x * a;
115 | r.y = p1.y * a;
116 | return r;
117 | }
118 |
119 | var vec2_ops = Operators({
120 | "+"(p1, p2) {
121 | var r = new Vec2();
122 | r.x = p1.x + p2.x;
123 | r.y = p1.y + p2.y;
124 | return r;
125 | },
126 | "-"(p1, p2) {
127 | var r = new Vec2();
128 | r.x = p1.x - p2.x;
129 | r.y = p1.y - p2.y;
130 | return r;
131 | },
132 | "=="(a, b) {
133 | return a.x == b.x && a.y == b.y;
134 | },
135 | "<"(a, b) {
136 | var r;
137 | /* lexicographic order */
138 | if (a.x == b.x)
139 | r = (a.y < b.y);
140 | else
141 | r = (a.x < b.x);
142 | return r;
143 | },
144 | "++"(a) {
145 | var r = new Vec2();
146 | r.x = a.x + 1;
147 | r.y = a.y + 1;
148 | return r;
149 | }
150 | },
151 | {
152 | left: Number,
153 | "*"(a, b) {
154 | return mul_scalar(b, a);
155 | }
156 | },
157 | {
158 | right: Number,
159 | "*"(a, b) {
160 | return mul_scalar(a, b);
161 | }
162 | });
163 |
164 | Vec2 = class Vec2 extends vec2_ops
165 | {
166 | constructor(x, y) {
167 | super();
168 | this.x = x;
169 | this.y = y;
170 | }
171 | toString() {
172 | return "Vec2(" + this.x + "," + this.y + ")";
173 | }
174 | }
175 |
176 | var a = new Vec2(1, 2);
177 | var b = new Vec2(3, 4);
178 | var r;
179 |
180 | r = a * 2 + 3 * b;
181 | assert(r.x === 11 && r.y === 16);
182 | assert(a == a, true);
183 | assert(a == b, false);
184 | assert(a != a, false);
185 | assert(a < b, true);
186 | assert(a <= b, true);
187 | assert(b < a, false);
188 | assert(b <= a, false);
189 | assert(a <= a, true);
190 | assert(a >= a, true);
191 | a++;
192 | assert(a.x === 2 && a.y === 3);
193 | r = ++a;
194 | assert(a.x === 3 && a.y === 4);
195 | assert(r === a);
196 | }
197 |
198 | function test_default_op()
199 | {
200 | assert(Object(1) + 2, 3);
201 | assert(Object(1) + true, 2);
202 | assert(-Object(1), -1);
203 | }
204 |
205 | test_operators_create();
206 | test_operators();
207 | test_default_op();
208 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_qjscalc.js:
--------------------------------------------------------------------------------
1 | "use math";
2 | "use strict";
3 |
4 | function assert(actual, expected, message) {
5 | if (arguments.length == 1)
6 | expected = true;
7 |
8 | if (actual === expected)
9 | return;
10 |
11 | if (actual !== null && expected !== null
12 | && typeof actual == 'object' && typeof expected == 'object'
13 | && actual.toString() === expected.toString())
14 | return;
15 |
16 | throw Error("assertion failed: got |" + actual + "|" +
17 | ", expected |" + expected + "|" +
18 | (message ? " (" + message + ")" : ""));
19 | }
20 |
21 | function assertThrows(err, func)
22 | {
23 | var ex;
24 | ex = false;
25 | try {
26 | func();
27 | } catch(e) {
28 | ex = true;
29 | assert(e instanceof err);
30 | }
31 | assert(ex, true, "exception expected");
32 | }
33 |
34 | // load more elaborate version of assert if available
35 | try { __loadScript("test_assert.js"); } catch(e) {}
36 |
37 | /*----------------*/
38 |
39 | function pow(a, n)
40 | {
41 | var r, i;
42 | r = 1;
43 | for(i = 0; i < n; i++)
44 | r *= a;
45 | return r;
46 | }
47 |
48 | function test_integer()
49 | {
50 | var a, r;
51 | a = pow(3, 100);
52 | assert((a - 1) != a);
53 | assert(a == 515377520732011331036461129765621272702107522001);
54 | assert(a == 0x5a4653ca673768565b41f775d6947d55cf3813d1);
55 | assert(Integer.isInteger(1) === true);
56 | assert(Integer.isInteger(1.0) === false);
57 |
58 | assert(Integer.floorLog2(0) === -1);
59 | assert(Integer.floorLog2(7) === 2);
60 |
61 | r = 1 << 31;
62 | assert(r, 2147483648, "1 << 31 === 2147483648");
63 |
64 | r = 1 << 32;
65 | assert(r, 4294967296, "1 << 32 === 4294967296");
66 |
67 | r = (1 << 31) < 0;
68 | assert(r, false, "(1 << 31) < 0 === false");
69 |
70 | assert(typeof 1 === "number");
71 | assert(typeof 9007199254740991 === "number");
72 | assert(typeof 9007199254740992 === "bigint");
73 | }
74 |
75 | function test_float()
76 | {
77 | assert(typeof 1.0 === "bigfloat");
78 | assert(1 == 1.0);
79 | assert(1 !== 1.0);
80 | }
81 |
82 | /* jscalc tests */
83 |
84 | function test_modulo()
85 | {
86 | var i, p, a, b;
87 |
88 | /* Euclidian modulo operator */
89 | assert((-3) % 2 == 1);
90 | assert(3 % (-2) == 1);
91 |
92 | p = 101;
93 | for(i = 1; i < p; i++) {
94 | a = Integer.invmod(i, p);
95 | assert(a >= 0 && a < p);
96 | assert((i * a) % p == 1);
97 | }
98 |
99 | assert(Integer.isPrime(2^107-1));
100 | assert(!Integer.isPrime((2^107-1) * (2^89-1)));
101 | a = Integer.factor((2^89-1)*2^3*11*13^2*1009);
102 | assert(a == [ 2,2,2,11,13,13,1009,618970019642690137449562111 ]);
103 | }
104 |
105 | function test_fraction()
106 | {
107 | assert((1/3 + 1).toString(), "4/3")
108 | assert((2/3)^30, 1073741824/205891132094649);
109 | assert(1/3 < 2/3);
110 | assert(1/3 < 1);
111 | assert(1/3 == 1.0/3);
112 | assert(1.0/3 < 2/3);
113 | }
114 |
115 | function test_mod()
116 | {
117 | var a, b, p;
118 |
119 | a = Mod(3, 101);
120 | b = Mod(-1, 101);
121 | assert((a + b) == Mod(2, 101));
122 | assert(a ^ 100 == Mod(1, 101));
123 |
124 | p = 2 ^ 607 - 1; /* mersenne prime */
125 | a = Mod(3, p) ^ (p - 1);
126 | assert(a == Mod(1, p));
127 | }
128 |
129 | function test_polynomial()
130 | {
131 | var a, b, q, r, t, i;
132 | a = (1 + X) ^ 4;
133 | assert(a == X^4+4*X^3+6*X^2+4*X+1);
134 |
135 | r = (1 + X);
136 | q = (1+X+X^2);
137 | b = (1 - X^2);
138 | a = q * b + r;
139 | t = Polynomial.divrem(a, b);
140 | assert(t[0] == q);
141 | assert(t[1] == r);
142 |
143 | a = 1 + 2*X + 3*X^2;
144 | assert(a.apply(0.1) == 1.23);
145 |
146 | a = 1-2*X^2+2*X^3;
147 | assert(deriv(a) == (6*X^2-4*X));
148 | assert(deriv(integ(a)) == a);
149 |
150 | a = (X-1)*(X-2)*(X-3)*(X-4)*(X-0.1);
151 | r = polroots(a);
152 | for(i = 0; i < r.length; i++) {
153 | b = abs(a.apply(r[i]));
154 | assert(b <= 1e-13);
155 | }
156 | }
157 |
158 | function test_poly_mod()
159 | {
160 | var a, p;
161 |
162 | /* modulo using polynomials */
163 | p = X^2 + X + 1;
164 | a = PolyMod(3+X, p) ^ 10;
165 | assert(a == PolyMod(-3725*X-18357, p));
166 |
167 | a = PolyMod(1/X, 1+X^2);
168 | assert(a == PolyMod(-X, X^2+1));
169 | }
170 |
171 | function test_rfunc()
172 | {
173 | var a;
174 | a = (X+1)/((X+1)*(X-1));
175 | assert(a == 1/(X-1));
176 | a = (X + 2) / (X - 2);
177 | assert(a.apply(1/3) == -7/5);
178 |
179 | assert(deriv((X^2-X+1)/(X-1)) == (X^2-2*X)/(X^2-2*X+1));
180 | }
181 |
182 | function test_series()
183 | {
184 | var a, b;
185 | a = 1+X+O(X^5);
186 | b = a.inverse();
187 | assert(b == 1-X+X^2-X^3+X^4+O(X^5));
188 | assert(deriv(b) == -1+2*X-3*X^2+4*X^3+O(X^4));
189 | assert(deriv(integ(b)) == b);
190 |
191 | a = Series(1/(1-X), 5);
192 | assert(a == 1+X+X^2+X^3+X^4+O(X^5));
193 | b = a.apply(0.1);
194 | assert(b == 1.1111);
195 |
196 | assert(exp(3*X^2+O(X^10)) == 1+3*X^2+9/2*X^4+9/2*X^6+27/8*X^8+O(X^10));
197 | assert(sin(X+O(X^6)) == X-1/6*X^3+1/120*X^5+O(X^6));
198 | assert(cos(X+O(X^6)) == 1-1/2*X^2+1/24*X^4+O(X^6));
199 | assert(tan(X+O(X^8)) == X+1/3*X^3+2/15*X^5+17/315*X^7+O(X^8));
200 | assert((1+X+O(X^6))^(2+X) == 1+2*X+2*X^2+3/2*X^3+5/6*X^4+5/12*X^5+O(X^6));
201 | }
202 |
203 | function test_matrix()
204 | {
205 | var a, b, r;
206 | a = [[1, 2],[3, 4]];
207 | b = [3, 4];
208 | r = a * b;
209 | assert(r == [11, 25]);
210 | r = (a^-1) * 2;
211 | assert(r == [[-4, 2],[3, -1]]);
212 |
213 | assert(norm2([1,2,3]) == 14);
214 |
215 | assert(diag([1,2,3]) == [ [ 1, 0, 0 ], [ 0, 2, 0 ], [ 0, 0, 3 ] ]);
216 | assert(trans(a) == [ [ 1, 3 ], [ 2, 4 ] ]);
217 | assert(trans([1,2,3]) == [[1,2,3]]);
218 | assert(trace(a) == 5);
219 |
220 | assert(charpoly(Matrix.hilbert(4)) == X^4-176/105*X^3+3341/12600*X^2-41/23625*X+1/6048000);
221 | assert(det(Matrix.hilbert(4)) == 1/6048000);
222 |
223 | a = [[1,2,1],[-2,-3,1],[3,5,0]];
224 | assert(rank(a) == 2);
225 | assert(ker(a) == [ [ 5 ], [ -3 ], [ 1 ] ]);
226 |
227 | assert(dp([1, 2, 3], [3, -4, -7]) === -26);
228 | assert(cp([1, 2, 3], [3, -4, -7]) == [ -2, 16, -10 ]);
229 | }
230 |
231 | function assert_eq(a, ref)
232 | {
233 | assert(abs(a / ref - 1.0) <= 1e-15);
234 | }
235 |
236 | function test_trig()
237 | {
238 | assert_eq(sin(1/2), 0.479425538604203);
239 | assert_eq(sin(2+3*I), 9.154499146911428-4.168906959966565*I);
240 | assert_eq(cos(2+3*I), -4.189625690968807-9.109227893755337*I);
241 | assert_eq((2+0.5*I)^(1.1-0.5*I), 2.494363021357619-0.23076804554558092*I);
242 | assert_eq(sqrt(2*I), 1 + I);
243 | }
244 |
245 | test_integer();
246 | test_float();
247 |
248 | test_modulo();
249 | test_fraction();
250 | test_mod();
251 | test_polynomial();
252 | test_poly_mod();
253 | test_rfunc();
254 | test_series();
255 | test_matrix();
256 | test_trig();
257 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/tests/test_std.js:
--------------------------------------------------------------------------------
1 | import * as std from "std";
2 | import * as os from "os";
3 |
4 | function assert(actual, expected, message) {
5 | if (arguments.length == 1)
6 | expected = true;
7 |
8 | if (actual === expected)
9 | return;
10 |
11 | if (actual !== null && expected !== null
12 | && typeof actual == 'object' && typeof expected == 'object'
13 | && actual.toString() === expected.toString())
14 | return;
15 |
16 | throw Error("assertion failed: got |" + actual + "|" +
17 | ", expected |" + expected + "|" +
18 | (message ? " (" + message + ")" : ""));
19 | }
20 |
21 | // load more elaborate version of assert if available
22 | try { std.loadScript("test_assert.js"); } catch(e) {}
23 |
24 | /*----------------*/
25 |
26 | function test_printf()
27 | {
28 | assert(std.sprintf("a=%d s=%s", 123, "abc"), "a=123 s=abc");
29 | }
30 |
31 | function test_file1()
32 | {
33 | var f, len, str, size, buf, ret, i, str1;
34 |
35 | f = std.tmpfile();
36 | str = "hello world\n";
37 | f.puts(str);
38 |
39 | f.seek(0, std.SEEK_SET);
40 | str1 = f.readAsString();
41 | assert(str1 === str);
42 |
43 | f.seek(0, std.SEEK_END);
44 | size = f.tell();
45 | assert(size === str.length);
46 |
47 | f.seek(0, std.SEEK_SET);
48 |
49 | buf = new Uint8Array(size);
50 | ret = f.read(buf.buffer, 0, size);
51 | assert(ret === size);
52 | for(i = 0; i < size; i++)
53 | assert(buf[i] === str.charCodeAt(i));
54 |
55 | f.close();
56 | }
57 |
58 | function test_file2()
59 | {
60 | var f, str, i, size;
61 | f = std.tmpfile();
62 | str = "hello world\n";
63 | size = str.length;
64 | for(i = 0; i < size; i++)
65 | f.putByte(str.charCodeAt(i));
66 | f.seek(0, std.SEEK_SET);
67 | for(i = 0; i < size; i++) {
68 | assert(str.charCodeAt(i) === f.getByte());
69 | }
70 | assert(f.getByte() === -1);
71 | f.close();
72 | }
73 |
74 | function test_getline()
75 | {
76 | var f, line, line_count, lines, i;
77 |
78 | lines = ["hello world", "line 1", "line 2" ];
79 | f = std.tmpfile();
80 | for(i = 0; i < lines.length; i++) {
81 | f.puts(lines[i], "\n");
82 | }
83 |
84 | f.seek(0, std.SEEK_SET);
85 | assert(!f.eof());
86 | line_count = 0;
87 | for(;;) {
88 | line = f.getline();
89 | if (line === null)
90 | break;
91 | assert(line == lines[line_count]);
92 | line_count++;
93 | }
94 | assert(f.eof());
95 | assert(line_count === lines.length);
96 |
97 | f.close();
98 | }
99 |
100 | function test_popen()
101 | {
102 | var str, f, fname = "tmp_file.txt";
103 | var content = "hello world";
104 |
105 | f = std.open(fname, "w");
106 | f.puts(content);
107 | f.close();
108 |
109 | /* test loadFile */
110 | assert(std.loadFile(fname), content);
111 |
112 | /* execute the 'cat' shell command */
113 | f = std.popen("cat " + fname, "r");
114 | str = f.readAsString();
115 | f.close();
116 |
117 | assert(str, content);
118 |
119 | os.remove(fname);
120 | }
121 |
122 | function test_os()
123 | {
124 | var fd, fpath, fname, fdir, buf, buf2, i, files, err, fdate, st, link_path;
125 |
126 | assert(os.isatty(0));
127 |
128 | fdir = "test_tmp_dir";
129 | fname = "tmp_file.txt";
130 | fpath = fdir + "/" + fname;
131 | link_path = fdir + "/test_link";
132 |
133 | os.remove(link_path);
134 | os.remove(fpath);
135 | os.remove(fdir);
136 |
137 | err = os.mkdir(fdir, 0o755);
138 | assert(err === 0);
139 |
140 | fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC);
141 | assert(fd >= 0);
142 |
143 | buf = new Uint8Array(10);
144 | for(i = 0; i < buf.length; i++)
145 | buf[i] = i;
146 | assert(os.write(fd, buf.buffer, 0, buf.length) === buf.length);
147 |
148 | assert(os.seek(fd, 0, std.SEEK_SET) === 0);
149 | buf2 = new Uint8Array(buf.length);
150 | assert(os.read(fd, buf2.buffer, 0, buf2.length) === buf2.length);
151 |
152 | for(i = 0; i < buf.length; i++)
153 | assert(buf[i] == buf2[i]);
154 |
155 | if (typeof BigInt !== "undefined") {
156 | assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6));
157 | assert(os.read(fd, buf2.buffer, 0, 1) === 1);
158 | assert(buf[6] == buf2[0]);
159 | }
160 |
161 | assert(os.close(fd) === 0);
162 |
163 | [files, err] = os.readdir(fdir);
164 | assert(err, 0);
165 | assert(files.indexOf(fname) >= 0);
166 |
167 | fdate = 10000;
168 |
169 | err = os.utimes(fpath, fdate, fdate);
170 | assert(err, 0);
171 |
172 | [st, err] = os.stat(fpath);
173 | assert(err, 0);
174 | assert(st.mode & os.S_IFMT, os.S_IFREG);
175 | assert(st.mtime, fdate);
176 |
177 | err = os.symlink(fname, link_path);
178 | assert(err === 0);
179 |
180 | [st, err] = os.lstat(link_path);
181 | assert(err, 0);
182 | assert(st.mode & os.S_IFMT, os.S_IFLNK);
183 |
184 | [buf, err] = os.readlink(link_path);
185 | assert(err, 0);
186 | assert(buf, fname);
187 |
188 | assert(os.remove(link_path) === 0);
189 |
190 | [buf, err] = os.getcwd();
191 | assert(err, 0);
192 |
193 | [buf2, err] = os.realpath(".");
194 | assert(err, 0);
195 |
196 | assert(buf, buf2);
197 |
198 | assert(os.remove(fpath) === 0);
199 |
200 | fd = os.open(fpath, os.O_RDONLY);
201 | assert(fd < 0);
202 |
203 | assert(os.remove(fdir) === 0);
204 | }
205 |
206 | function test_os_exec()
207 | {
208 | var ret, fds, pid, f, status;
209 |
210 | ret = os.exec(["true"]);
211 | assert(ret, 0);
212 |
213 | ret = os.exec(["/bin/sh", "-c", "exit 1"], { usePath: false });
214 | assert(ret, 1);
215 |
216 | fds = os.pipe();
217 | pid = os.exec(["echo", "hello"], { stdout: fds[1], block: false } );
218 | assert(pid >= 0);
219 | os.close(fds[1]); /* close the write end (as it is only in the child) */
220 | f = std.fdopen(fds[0], "r");
221 | assert(f.getline(), "hello");
222 | assert(f.getline(), null);
223 | f.close();
224 | [ret, status] = os.waitpid(pid, 0);
225 | assert(ret, pid);
226 | assert(status & 0x7f, 0); /* exited */
227 | assert(status >> 8, 0); /* exit code */
228 |
229 | pid = os.exec(["cat"], { block: false } );
230 | assert(pid >= 0);
231 | os.kill(pid, os.SIGQUIT);
232 | [ret, status] = os.waitpid(pid, 0);
233 | assert(ret, pid);
234 | assert(status & 0x7f, os.SIGQUIT);
235 | }
236 |
237 | function test_timer()
238 | {
239 | var th, i;
240 |
241 | /* just test that a timer can be inserted and removed */
242 | th = [];
243 | for(i = 0; i < 3; i++)
244 | th[i] = os.setTimeout(function () { }, 1000);
245 | for(i = 0; i < 3; i++)
246 | os.clearTimeout(th[i]);
247 | }
248 |
249 | test_printf();
250 | test_file1();
251 | test_file2();
252 | test_getline();
253 | test_popen();
254 | test_os();
255 | test_os_exec();
256 | test_timer();
257 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/unicode_download.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | set -e
3 |
4 | url="ftp://ftp.unicode.org/Public/13.0.0/ucd"
5 | emoji_url="${url}/emoji/emoji-data.txt"
6 |
7 | files="CaseFolding.txt DerivedNormalizationProps.txt PropList.txt \
8 | SpecialCasing.txt CompositionExclusions.txt ScriptExtensions.txt \
9 | UnicodeData.txt DerivedCoreProperties.txt NormalizationTest.txt Scripts.txt \
10 | PropertyValueAliases.txt"
11 |
12 | mkdir -p unicode
13 |
14 | #for f in $files; do
15 | # g="${url}/${f}"
16 | # wget $g -O unicode/$f
17 | #done
18 |
19 | wget $emoji_url -O unicode/emoji-data.txt
20 |
--------------------------------------------------------------------------------
/lib/quickjs-2020-03-16/unicode_gen_def.h:
--------------------------------------------------------------------------------
1 | #ifdef UNICODE_GENERAL_CATEGORY
2 | DEF(Cn, "Unassigned") /* must be zero */
3 | DEF(Lu, "Uppercase_Letter")
4 | DEF(Ll, "Lowercase_Letter")
5 | DEF(Lt, "Titlecase_Letter")
6 | DEF(Lm, "Modifier_Letter")
7 | DEF(Lo, "Other_Letter")
8 | DEF(Mn, "Nonspacing_Mark")
9 | DEF(Mc, "Spacing_Mark")
10 | DEF(Me, "Enclosing_Mark")
11 | DEF(Nd, "Decimal_Number,digit")
12 | DEF(Nl, "Letter_Number")
13 | DEF(No, "Other_Number")
14 | DEF(Sm, "Math_Symbol")
15 | DEF(Sc, "Currency_Symbol")
16 | DEF(Sk, "Modifier_Symbol")
17 | DEF(So, "Other_Symbol")
18 | DEF(Pc, "Connector_Punctuation")
19 | DEF(Pd, "Dash_Punctuation")
20 | DEF(Ps, "Open_Punctuation")
21 | DEF(Pe, "Close_Punctuation")
22 | DEF(Pi, "Initial_Punctuation")
23 | DEF(Pf, "Final_Punctuation")
24 | DEF(Po, "Other_Punctuation")
25 | DEF(Zs, "Space_Separator")
26 | DEF(Zl, "Line_Separator")
27 | DEF(Zp, "Paragraph_Separator")
28 | DEF(Cc, "Control,cntrl")
29 | DEF(Cf, "Format")
30 | DEF(Cs, "Surrogate")
31 | DEF(Co, "Private_Use")
32 | /* synthetic properties */
33 | DEF(LC, "Cased_Letter")
34 | DEF(L, "Letter")
35 | DEF(M, "Mark,Combining_Mark")
36 | DEF(N, "Number")
37 | DEF(S, "Symbol")
38 | DEF(P, "Punctuation,punct")
39 | DEF(Z, "Separator")
40 | DEF(C, "Other")
41 | #endif
42 |
43 | #ifdef UNICODE_SCRIPT
44 | /* scripts aliases names in PropertyValueAliases.txt */
45 | DEF(Unknown, "Zzzz")
46 | DEF(Adlam, "Adlm")
47 | DEF(Ahom, "Ahom")
48 | DEF(Anatolian_Hieroglyphs, "Hluw")
49 | DEF(Arabic, "Arab")
50 | DEF(Armenian, "Armn")
51 | DEF(Avestan, "Avst")
52 | DEF(Balinese, "Bali")
53 | DEF(Bamum, "Bamu")
54 | DEF(Bassa_Vah, "Bass")
55 | DEF(Batak, "Batk")
56 | DEF(Bengali, "Beng")
57 | DEF(Bhaiksuki, "Bhks")
58 | DEF(Bopomofo, "Bopo")
59 | DEF(Brahmi, "Brah")
60 | DEF(Braille, "Brai")
61 | DEF(Buginese, "Bugi")
62 | DEF(Buhid, "Buhd")
63 | DEF(Canadian_Aboriginal, "Cans")
64 | DEF(Carian, "Cari")
65 | DEF(Caucasian_Albanian, "Aghb")
66 | DEF(Chakma, "Cakm")
67 | DEF(Cham, "Cham")
68 | DEF(Cherokee, "Cher")
69 | DEF(Chorasmian, "Chrs")
70 | DEF(Common, "Zyyy")
71 | DEF(Coptic, "Copt,Qaac")
72 | DEF(Cuneiform, "Xsux")
73 | DEF(Cypriot, "Cprt")
74 | DEF(Cyrillic, "Cyrl")
75 | DEF(Deseret, "Dsrt")
76 | DEF(Devanagari, "Deva")
77 | DEF(Dives_Akuru, "Diak")
78 | DEF(Dogra, "Dogr")
79 | DEF(Duployan, "Dupl")
80 | DEF(Egyptian_Hieroglyphs, "Egyp")
81 | DEF(Elbasan, "Elba")
82 | DEF(Elymaic, "Elym")
83 | DEF(Ethiopic, "Ethi")
84 | DEF(Georgian, "Geor")
85 | DEF(Glagolitic, "Glag")
86 | DEF(Gothic, "Goth")
87 | DEF(Grantha, "Gran")
88 | DEF(Greek, "Grek")
89 | DEF(Gujarati, "Gujr")
90 | DEF(Gunjala_Gondi, "Gong")
91 | DEF(Gurmukhi, "Guru")
92 | DEF(Han, "Hani")
93 | DEF(Hangul, "Hang")
94 | DEF(Hanifi_Rohingya, "Rohg")
95 | DEF(Hanunoo, "Hano")
96 | DEF(Hatran, "Hatr")
97 | DEF(Hebrew, "Hebr")
98 | DEF(Hiragana, "Hira")
99 | DEF(Imperial_Aramaic, "Armi")
100 | DEF(Inherited, "Zinh,Qaai")
101 | DEF(Inscriptional_Pahlavi, "Phli")
102 | DEF(Inscriptional_Parthian, "Prti")
103 | DEF(Javanese, "Java")
104 | DEF(Kaithi, "Kthi")
105 | DEF(Kannada, "Knda")
106 | DEF(Katakana, "Kana")
107 | DEF(Kayah_Li, "Kali")
108 | DEF(Kharoshthi, "Khar")
109 | DEF(Khmer, "Khmr")
110 | DEF(Khojki, "Khoj")
111 | DEF(Khitan_Small_Script, "Kits")
112 | DEF(Khudawadi, "Sind")
113 | DEF(Lao, "Laoo")
114 | DEF(Latin, "Latn")
115 | DEF(Lepcha, "Lepc")
116 | DEF(Limbu, "Limb")
117 | DEF(Linear_A, "Lina")
118 | DEF(Linear_B, "Linb")
119 | DEF(Lisu, "Lisu")
120 | DEF(Lycian, "Lyci")
121 | DEF(Lydian, "Lydi")
122 | DEF(Makasar, "Maka")
123 | DEF(Mahajani, "Mahj")
124 | DEF(Malayalam, "Mlym")
125 | DEF(Mandaic, "Mand")
126 | DEF(Manichaean, "Mani")
127 | DEF(Marchen, "Marc")
128 | DEF(Masaram_Gondi, "Gonm")
129 | DEF(Medefaidrin, "Medf")
130 | DEF(Meetei_Mayek, "Mtei")
131 | DEF(Mende_Kikakui, "Mend")
132 | DEF(Meroitic_Cursive, "Merc")
133 | DEF(Meroitic_Hieroglyphs, "Mero")
134 | DEF(Miao, "Plrd")
135 | DEF(Modi, "Modi")
136 | DEF(Mongolian, "Mong")
137 | DEF(Mro, "Mroo")
138 | DEF(Multani, "Mult")
139 | DEF(Myanmar, "Mymr")
140 | DEF(Nabataean, "Nbat")
141 | DEF(Nandinagari, "Nand")
142 | DEF(New_Tai_Lue, "Talu")
143 | DEF(Newa, "Newa")
144 | DEF(Nko, "Nkoo")
145 | DEF(Nushu, "Nshu")
146 | DEF(Nyiakeng_Puachue_Hmong, "Hmnp")
147 | DEF(Ogham, "Ogam")
148 | DEF(Ol_Chiki, "Olck")
149 | DEF(Old_Hungarian, "Hung")
150 | DEF(Old_Italic, "Ital")
151 | DEF(Old_North_Arabian, "Narb")
152 | DEF(Old_Permic, "Perm")
153 | DEF(Old_Persian, "Xpeo")
154 | DEF(Old_Sogdian, "Sogo")
155 | DEF(Old_South_Arabian, "Sarb")
156 | DEF(Old_Turkic, "Orkh")
157 | DEF(Oriya, "Orya")
158 | DEF(Osage, "Osge")
159 | DEF(Osmanya, "Osma")
160 | DEF(Pahawh_Hmong, "Hmng")
161 | DEF(Palmyrene, "Palm")
162 | DEF(Pau_Cin_Hau, "Pauc")
163 | DEF(Phags_Pa, "Phag")
164 | DEF(Phoenician, "Phnx")
165 | DEF(Psalter_Pahlavi, "Phlp")
166 | DEF(Rejang, "Rjng")
167 | DEF(Runic, "Runr")
168 | DEF(Samaritan, "Samr")
169 | DEF(Saurashtra, "Saur")
170 | DEF(Sharada, "Shrd")
171 | DEF(Shavian, "Shaw")
172 | DEF(Siddham, "Sidd")
173 | DEF(SignWriting, "Sgnw")
174 | DEF(Sinhala, "Sinh")
175 | DEF(Sogdian, "Sogd")
176 | DEF(Sora_Sompeng, "Sora")
177 | DEF(Soyombo, "Soyo")
178 | DEF(Sundanese, "Sund")
179 | DEF(Syloti_Nagri, "Sylo")
180 | DEF(Syriac, "Syrc")
181 | DEF(Tagalog, "Tglg")
182 | DEF(Tagbanwa, "Tagb")
183 | DEF(Tai_Le, "Tale")
184 | DEF(Tai_Tham, "Lana")
185 | DEF(Tai_Viet, "Tavt")
186 | DEF(Takri, "Takr")
187 | DEF(Tamil, "Taml")
188 | DEF(Tangut, "Tang")
189 | DEF(Telugu, "Telu")
190 | DEF(Thaana, "Thaa")
191 | DEF(Thai, "Thai")
192 | DEF(Tibetan, "Tibt")
193 | DEF(Tifinagh, "Tfng")
194 | DEF(Tirhuta, "Tirh")
195 | DEF(Ugaritic, "Ugar")
196 | DEF(Vai, "Vaii")
197 | DEF(Wancho, "Wcho")
198 | DEF(Warang_Citi, "Wara")
199 | DEF(Yezidi, "Yezi")
200 | DEF(Yi, "Yiii")
201 | DEF(Zanabazar_Square, "Zanb")
202 | #endif
203 |
204 | #ifdef UNICODE_PROP_LIST
205 | /* Prop list not exported to regexp */
206 | DEF(Hyphen, "")
207 | DEF(Other_Math, "")
208 | DEF(Other_Alphabetic, "")
209 | DEF(Other_Lowercase, "")
210 | DEF(Other_Uppercase, "")
211 | DEF(Other_Grapheme_Extend, "")
212 | DEF(Other_Default_Ignorable_Code_Point, "")
213 | DEF(Other_ID_Start, "")
214 | DEF(Other_ID_Continue, "")
215 | DEF(Prepended_Concatenation_Mark, "")
216 | /* additional computed properties for smaller tables */
217 | DEF(ID_Continue1, "")
218 | DEF(XID_Start1, "")
219 | DEF(XID_Continue1, "")
220 | DEF(Changes_When_Titlecased1, "")
221 | DEF(Changes_When_Casefolded1, "")
222 | DEF(Changes_When_NFKC_Casefolded1, "")
223 |
224 | /* Prop list exported to JS */
225 | DEF(ASCII_Hex_Digit, "AHex")
226 | DEF(Bidi_Control, "Bidi_C")
227 | DEF(Dash, "")
228 | DEF(Deprecated, "Dep")
229 | DEF(Diacritic, "Dia")
230 | DEF(Extender, "Ext")
231 | DEF(Hex_Digit, "Hex")
232 | DEF(IDS_Binary_Operator, "IDSB")
233 | DEF(IDS_Trinary_Operator, "IDST")
234 | DEF(Ideographic, "Ideo")
235 | DEF(Join_Control, "Join_C")
236 | DEF(Logical_Order_Exception, "LOE")
237 | DEF(Noncharacter_Code_Point, "NChar")
238 | DEF(Pattern_Syntax, "Pat_Syn")
239 | DEF(Pattern_White_Space, "Pat_WS")
240 | DEF(Quotation_Mark, "QMark")
241 | DEF(Radical, "")
242 | DEF(Regional_Indicator, "RI")
243 | DEF(Sentence_Terminal, "STerm")
244 | DEF(Soft_Dotted, "SD")
245 | DEF(Terminal_Punctuation, "Term")
246 | DEF(Unified_Ideograph, "UIdeo")
247 | DEF(Variation_Selector, "VS")
248 | DEF(White_Space, "space")
249 | DEF(Bidi_Mirrored, "Bidi_M")
250 | DEF(Emoji, "")
251 | DEF(Emoji_Component, "EComp")
252 | DEF(Emoji_Modifier, "EMod")
253 | DEF(Emoji_Modifier_Base, "EBase")
254 | DEF(Emoji_Presentation, "EPres")
255 | DEF(Extended_Pictographic, "ExtPict")
256 | DEF(Default_Ignorable_Code_Point, "DI")
257 | DEF(ID_Start, "IDS")
258 | DEF(Case_Ignorable, "CI")
259 |
260 | /* other binary properties */
261 | DEF(ASCII,"")
262 | DEF(Alphabetic, "Alpha")
263 | DEF(Any, "")
264 | DEF(Assigned,"")
265 | DEF(Cased, "")
266 | DEF(Changes_When_Casefolded, "CWCF")
267 | DEF(Changes_When_Casemapped, "CWCM")
268 | DEF(Changes_When_Lowercased, "CWL")
269 | DEF(Changes_When_NFKC_Casefolded, "CWKCF")
270 | DEF(Changes_When_Titlecased, "CWT")
271 | DEF(Changes_When_Uppercased, "CWU")
272 | DEF(Grapheme_Base, "Gr_Base")
273 | DEF(Grapheme_Extend, "Gr_Ext")
274 | DEF(ID_Continue, "IDC")
275 | DEF(Lowercase, "Lower")
276 | DEF(Math, "")
277 | DEF(Uppercase, "Upper")
278 | DEF(XID_Continue, "XIDC")
279 | DEF(XID_Start, "XIDS")
280 |
281 | /* internal tables with index */
282 | DEF(Cased1, "")
283 |
284 | #endif
285 |
--------------------------------------------------------------------------------
/package-lock.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "safe-wasm-jsvm",
3 | "version": "1.0.0",
4 | "lockfileVersion": 1,
5 | "requires": true,
6 | "dependencies": {
7 | "async": {
8 | "version": "2.6.3",
9 | "resolved": "https://registry.npmjs.org/async/-/async-2.6.3.tgz",
10 | "integrity": "sha512-zflvls11DCy+dQWzTW2dzuilv8Z5X/pjfmZOWba6TNIVDm+2UDaJmXSOXlasHKfNBs8oo3M0aT50fDEWfKZjXg==",
11 | "requires": {
12 | "lodash": "^4.17.14"
13 | }
14 | },
15 | "basic-auth": {
16 | "version": "1.1.0",
17 | "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-1.1.0.tgz",
18 | "integrity": "sha1-RSIe5Cn37h5QNb4/UVM/HN/SmIQ="
19 | },
20 | "colors": {
21 | "version": "1.4.0",
22 | "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
23 | "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
24 | },
25 | "corser": {
26 | "version": "2.0.1",
27 | "resolved": "https://registry.npmjs.org/corser/-/corser-2.0.1.tgz",
28 | "integrity": "sha1-jtolLsqrWEDc2XXOuQ2TcMgZ/4c="
29 | },
30 | "debug": {
31 | "version": "3.2.6",
32 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
33 | "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
34 | "requires": {
35 | "ms": "^2.1.1"
36 | }
37 | },
38 | "ecstatic": {
39 | "version": "3.3.2",
40 | "resolved": "https://registry.npmjs.org/ecstatic/-/ecstatic-3.3.2.tgz",
41 | "integrity": "sha512-fLf9l1hnwrHI2xn9mEDT7KIi22UDqA2jaCwyCbSUJh9a1V+LEUSL/JO/6TIz/QyuBURWUHrFL5Kg2TtO1bkkog==",
42 | "requires": {
43 | "he": "^1.1.1",
44 | "mime": "^1.6.0",
45 | "minimist": "^1.1.0",
46 | "url-join": "^2.0.5"
47 | }
48 | },
49 | "eventemitter3": {
50 | "version": "4.0.0",
51 | "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.0.tgz",
52 | "integrity": "sha512-qerSRB0p+UDEssxTtm6EDKcE7W4OaoisfIMl4CngyEhjpYglocpNg6UEqCvemdGhosAsg4sO2dXJOdyBifPGCg=="
53 | },
54 | "follow-redirects": {
55 | "version": "1.10.0",
56 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.10.0.tgz",
57 | "integrity": "sha512-4eyLK6s6lH32nOvLLwlIOnr9zrL8Sm+OvW4pVTJNoXeGzYIkHVf+pADQi+OJ0E67hiuSLezPVPyBcIZO50TmmQ==",
58 | "requires": {
59 | "debug": "^3.0.0"
60 | }
61 | },
62 | "he": {
63 | "version": "1.2.0",
64 | "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
65 | "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw=="
66 | },
67 | "http-proxy": {
68 | "version": "1.18.0",
69 | "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.0.tgz",
70 | "integrity": "sha512-84I2iJM/n1d4Hdgc6y2+qY5mDaz2PUVjlg9znE9byl+q0uC3DeByqBGReQu5tpLK0TAqTIXScRUV+dg7+bUPpQ==",
71 | "requires": {
72 | "eventemitter3": "^4.0.0",
73 | "follow-redirects": "^1.0.0",
74 | "requires-port": "^1.0.0"
75 | }
76 | },
77 | "http-server": {
78 | "version": "0.12.1",
79 | "resolved": "https://registry.npmjs.org/http-server/-/http-server-0.12.1.tgz",
80 | "integrity": "sha512-T0jB+7J7GJ2Vo+a4/T7P7SbQ3x2GPDnqRqQXdfEuPuUOmES/9NBxPnDm7dh1HGEeUWqUmLUNtGV63ZC5Uy3tGA==",
81 | "requires": {
82 | "basic-auth": "^1.0.3",
83 | "colors": "^1.3.3",
84 | "corser": "^2.0.1",
85 | "ecstatic": "^3.3.2",
86 | "http-proxy": "^1.17.0",
87 | "opener": "^1.5.1",
88 | "optimist": "~0.6.1",
89 | "portfinder": "^1.0.20",
90 | "secure-compare": "3.0.1",
91 | "union": "~0.5.0"
92 | }
93 | },
94 | "lodash": {
95 | "version": "4.17.15",
96 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
97 | "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
98 | },
99 | "mime": {
100 | "version": "1.6.0",
101 | "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
102 | "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
103 | },
104 | "minimist": {
105 | "version": "1.2.5",
106 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
107 | "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
108 | },
109 | "mkdirp": {
110 | "version": "0.5.4",
111 | "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.4.tgz",
112 | "integrity": "sha512-iG9AK/dJLtJ0XNgTuDbSyNS3zECqDlAhnQW4CsNxBG3LQJBbHmRX1egw39DmtOdCAqY+dKXV+sgPgilNWUKMVw==",
113 | "requires": {
114 | "minimist": "^1.2.5"
115 | }
116 | },
117 | "ms": {
118 | "version": "2.1.2",
119 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
120 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
121 | },
122 | "opener": {
123 | "version": "1.5.1",
124 | "resolved": "https://registry.npmjs.org/opener/-/opener-1.5.1.tgz",
125 | "integrity": "sha512-goYSy5c2UXE4Ra1xixabeVh1guIX/ZV/YokJksb6q2lubWu6UbvPQ20p542/sFIll1nl8JnCyK9oBaOcCWXwvA=="
126 | },
127 | "optimist": {
128 | "version": "0.6.1",
129 | "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz",
130 | "integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=",
131 | "requires": {
132 | "minimist": "~0.0.1",
133 | "wordwrap": "~0.0.2"
134 | },
135 | "dependencies": {
136 | "minimist": {
137 | "version": "0.0.10",
138 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz",
139 | "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8="
140 | }
141 | }
142 | },
143 | "portfinder": {
144 | "version": "1.0.25",
145 | "resolved": "https://registry.npmjs.org/portfinder/-/portfinder-1.0.25.tgz",
146 | "integrity": "sha512-6ElJnHBbxVA1XSLgBp7G1FiCkQdlqGzuF7DswL5tcea+E8UpuvPU7beVAjjRwCioTS9ZluNbu+ZyRvgTsmqEBg==",
147 | "requires": {
148 | "async": "^2.6.2",
149 | "debug": "^3.1.1",
150 | "mkdirp": "^0.5.1"
151 | }
152 | },
153 | "qs": {
154 | "version": "6.9.2",
155 | "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.2.tgz",
156 | "integrity": "sha512-2eQ6zajpK7HwqrY1rRtGw5IZvjgtELXzJECaEDuzDFo2jjnIXpJSimzd4qflWZq6bLLi+Zgfj5eDrAzl/lptyg=="
157 | },
158 | "requires-port": {
159 | "version": "1.0.0",
160 | "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
161 | "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
162 | },
163 | "secure-compare": {
164 | "version": "3.0.1",
165 | "resolved": "https://registry.npmjs.org/secure-compare/-/secure-compare-3.0.1.tgz",
166 | "integrity": "sha1-8aAymzCLIh+uN7mXTz1XjQypmeM="
167 | },
168 | "union": {
169 | "version": "0.5.0",
170 | "resolved": "https://registry.npmjs.org/union/-/union-0.5.0.tgz",
171 | "integrity": "sha512-N6uOhuW6zO95P3Mel2I2zMsbsanvvtgn6jVqJv4vbVcz/JN0OkL9suomjQGmWtxJQXOCqUJvquc1sMeNz/IwlA==",
172 | "requires": {
173 | "qs": "^6.4.0"
174 | }
175 | },
176 | "url-join": {
177 | "version": "2.0.5",
178 | "resolved": "https://registry.npmjs.org/url-join/-/url-join-2.0.5.tgz",
179 | "integrity": "sha1-WvIvGMBSoACkjXuCxenC4v7tpyg="
180 | },
181 | "wordwrap": {
182 | "version": "0.0.3",
183 | "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
184 | "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
185 | }
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "safe-wasm-jsvm",
3 | "version": "1.0.0",
4 | "description": "wasm 前端安全, Quickjs",
5 | "main": "index.js",
6 | "scripts": {
7 | "server": "http-server ./public -p 8088 -g --cors -o",
8 | "asm": "./config/asm_build.sh",
9 | "wasm": "./config/wasm_build.sh",
10 | "test": "echo \"Error: no test specified\" && exit 1"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "git@github.com:1160007652/safe-wasm-jsvm.git"
15 | },
16 | "keywords": [
17 | "wasm",
18 | "前端安全",
19 | "Quickjs"
20 | ],
21 | "author": "1160007652@qq.com",
22 | "license": "ISC",
23 | "dependencies": {
24 | "http-server": "^0.12.1"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/public/build/main-asm.js.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1160007652/safe-wasm-jsvm/b03e2f28c062e902d3c8d053eca7662fe9d0ad3d/public/build/main-asm.js.gz
--------------------------------------------------------------------------------
/public/build/main-wasm.js.gz:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1160007652/safe-wasm-jsvm/b03e2f28c062e902d3c8d053eca7662fe9d0ad3d/public/build/main-wasm.js.gz
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1160007652/safe-wasm-jsvm/b03e2f28c062e902d3c8d053eca7662fe9d0ad3d/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | 安全码WASM校验
9 |
10 |
11 |
12 | function say(){
13 | const a = {a: 1, b: 2};
14 | const b = {c:0 , ...a};
15 |
16 | return \`HelloWord - \${Object.keys(b)} - \${Object.values(b)}\`;
17 | }
18 |
19 | say();
20 |
21 | 结果:
22 |
23 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/js/index.js:
--------------------------------------------------------------------------------
1 | function loadScript(url,callback = undefined) {
2 | var script=document.createElement("script");
3 | script.type="text/javascript";
4 | if(script.readyState){ // ie游览器
5 | script.onreadystatechange=function () {
6 | if(script.readyState=="loaded" || script.readyState=="complete"){
7 | script.onreadystatechange=null;
8 | callback && callback();
9 | }
10 | }
11 | }else { //其他
12 | script.onload=function () {
13 | callback && callback();
14 | }
15 | };
16 | script.src=url;
17 | document.getElementsByTagName("head")[0].appendChild(script);
18 | }
19 |
20 | if (false && window.WebAssembly) {
21 | // 支持WASM浏览器的使用方式
22 | loadScript("../build/main-wasm.js");
23 | } else {
24 | // 不支持WASM浏览器的使用方式
25 | loadScript("//cdn.bootcss.com/babel-polyfill/7.4.4/polyfill.min.js", function(){
26 | loadScript("../build/main-asm.js");
27 | });
28 | }
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 |
2 | #### wasm 兼容性:
3 |
4 | https://caniuse.com/#feat=wasm
5 |
6 | -----------------------
7 | #### 启动 server 服务:
8 |
9 | > 安装依赖:npm i
10 |
11 | > 运行服务:npm run server
12 |
13 | ---------------------
14 |
15 | #### wasm转wast
16 | 1. 首先安装wasm2wat库
17 | 2. wasm2wat main.wasm -o main.wast
18 |
19 | ----------------------
20 |
21 | #### wasm 编译命令
22 |
23 | 1. 首先安装 docker 容器
24 | > https://www.docker.com/
25 | 2. 安装基于 docker 的 [emcc](https://hub.docker.com/r/apiaryio/emcc) 镜像
26 | > docker pull apiaryio/emcc
27 | 3. 开始编译
28 | > 执行 wasm 编译命令: npm run wasm
29 |
30 | > 执行 asm 编译命令: npm run asm
31 |
32 | ------------------------
33 |
34 | #### 目录结构
35 | - config/ 配置项目录
36 | - config/asm_build.sh asm脚本编译命令
37 | - config/wasm_build.sh wasm脚本编译命令
38 | - doc/ 文档, 图片
39 | - lib/ 外部依赖库
40 | - public/ 启动服务运行的目录
41 | - public/build/ 保存构建成功的main-[wasm|asm].[js,js.gz]文件
42 | - public/js/ js代码目录
43 | - public/index.html 入口
44 | - src-wasm/ C++源码目录
45 | - src-wasm/main.cpp wasm 源码
46 |
47 | #### wasm 运行图
48 |
49 | 
50 | 
51 |
52 | #### asm 运行图
53 |
54 | 
55 | 
56 |
--------------------------------------------------------------------------------
/src-wasm/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "../lib/quickjs-2020-03-16/quickjs.h"
4 |
5 | #ifndef EM_PORT_API
6 | # if defined(__EMSCRIPTEN__)
7 | # if defined(__cplusplus)
8 | # define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
9 | # else
10 | # define EM_PORT_API(rettype) rettype EMSCRIPTEN_KEEPALIVE
11 | # endif
12 | # else
13 | # if defined(__cplusplus)
14 | # define EM_PORT_API(rettype) extern "C" rettype
15 | # else
16 | # define EM_PORT_API(rettype) rettype
17 | # endif
18 | # endif
19 | #endif
20 |
21 | // 导出 api 供 js 调用
22 | EM_PORT_API(const char*) eval(const char* str) {
23 |
24 | // 运行时环境
25 | JSRuntime* runtime = JS_NewRuntime();
26 |
27 | // 获取运行上下文
28 | JSContext* ctx = JS_NewContext(runtime);
29 |
30 | // 在此上下文中eval 执行js 代码
31 | JSValue result = JS_Eval(ctx, str, strlen(str), "", JS_EVAL_TYPE_GLOBAL);
32 |
33 | // 异常情况
34 | if (JS_IsException(result)) {
35 | JSValue realException = JS_GetException(ctx);
36 | return JS_ToCString(ctx, realException);
37 | }
38 |
39 | JSValue json = JS_JSONStringify(ctx, result, JS_UNDEFINED, JS_UNDEFINED);
40 |
41 | // 释放内存
42 | JS_FreeValue(ctx, result);
43 |
44 | return JS_ToCString(ctx, json);
45 |
46 | }
47 |
--------------------------------------------------------------------------------