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
js_bjson_read(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)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
js_bjson_write(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)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
js_bjson_init(JSContext * ctx,JSModuleDef * m)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
JS_INIT_MODULE(JSContext * ctx,const char * module_name)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