update options for sanitizer
[liba.git] / quickjs / src / quickjs.c
blobf30207c592d877355c41831b25626411808c7b0a
1 #include "a.h"
3 JSValue js_concat(JSContext *ctx, JSValueConst val)
5 JSValue this_val = JS_NewArray(ctx);
6 JSValueConst argv[] = {this_val, val};
7 JSValue concat = JS_GetPropertyStr(ctx, this_val, "concat");
8 JSValue apply = JS_GetPropertyStr(ctx, concat, "apply");
9 JSValue res = JS_Call(ctx, apply, this_val, A_LEN(argv), argv);
10 JS_FreeValue(ctx, this_val);
11 JS_FreeValue(ctx, concat);
12 JS_FreeValue(ctx, apply);
13 return res;
16 int js_array_length(JSContext *ctx, JSValueConst val, a_u32 *plen)
18 JSValue length = JS_GetPropertyStr(ctx, val, "length");
19 if (JS_IsException(length)) { return ~0; }
20 int ret = JS_ToUint32(ctx, plen, length);
21 JS_FreeValue(ctx, length);
22 return ret;
25 JSValue js_array_u8_new(JSContext *ctx, a_u8 const *ptr, a_u32 len)
27 JSValue val = JS_NewArray(ctx);
28 if (JS_IsException(val)) { return val; }
29 for (unsigned int i = 0; i < len; ++i)
31 JS_SetPropertyUint32(ctx, val, i, JS_NewInt32(ctx, (a_u8)ptr[i]));
33 return val;
36 JSValue js_array_u16_new(JSContext *ctx, a_u16 const *ptr, a_u32 len)
38 JSValue val = JS_NewArray(ctx);
39 if (JS_IsException(val)) { return val; }
40 for (unsigned int i = 0; i < len; ++i)
42 JS_SetPropertyUint32(ctx, val, i, JS_NewInt32(ctx, (a_u8)ptr[i]));
44 return val;
47 JSValue js_array_u32_new(JSContext *ctx, a_u32 const *ptr, a_u32 len)
49 JSValue val = JS_NewArray(ctx);
50 if (JS_IsException(val)) { return val; }
51 for (unsigned int i = 0; i < len; ++i)
53 JS_SetPropertyUint32(ctx, val, i, JS_NewUint32(ctx, ptr[i]));
55 return val;
58 JSValue js_array_u64_new(JSContext *ctx, a_u64 const *ptr, a_u32 len)
60 JSValue val = JS_NewArray(ctx);
61 if (JS_IsException(val)) { return val; }
62 for (unsigned int i = 0; i < len; ++i)
64 JS_SetPropertyUint32(ctx, val, i, JS_NewBigUint64(ctx, ptr[i]));
66 return val;
69 JSValue js_array_num_new(JSContext *ctx, a_float const *ptr, a_u32 len)
71 JSValue val = JS_NewArray(ctx);
72 if (JS_IsException(val)) { return val; }
73 for (unsigned int i = 0; i < len; ++i)
75 JS_SetPropertyUint32(ctx, val, i, JS_NewFloat64(ctx, (double)ptr[i]));
77 return val;
80 int js_array_num_get(JSContext *ctx, JSValueConst val, a_float *ptr, a_u32 len)
82 for (unsigned int i = 0; i < len; ++i)
84 JSValue _x = JS_GetPropertyUint32(ctx, val, i);
85 if (JS_IsException(_x)) { return ~0; }
86 double x;
87 int r = JS_ToFloat64(ctx, &x, _x);
88 JS_FreeValue(ctx, _x);
89 if (r) { return r; }
90 ptr[i] = (a_float)x;
92 return 0;