Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ jobs:
run: |
cp build/fib.so examples/
cp build/point.so examples/
cp build/bjson.so tests/
./build/qjs examples/test_fib.js
./build/qjs examples/test_point.js
./build/qjs tests/test_bjson.js
Expand Down Expand Up @@ -207,6 +206,7 @@ jobs:
- name: test
run: |
build\${{matrix.buildType}}\qjs.exe tests\test_bigint.js
build\${{matrix.buildType}}\qjs.exe tests\test_bjson.js
build\${{matrix.buildType}}\qjs.exe tests\test_closure.js
build\${{matrix.buildType}}\qjs.exe tests\test_language.js
build\${{matrix.buildType}}\qjs.exe tests\test_builtin.js
Expand Down Expand Up @@ -235,6 +235,7 @@ jobs:
- name: test
run: |
build\${{matrix.buildType}}\qjs.exe tests\test_bigint.js
build\${{matrix.buildType}}\qjs.exe tests\test_bjson.js
build\${{matrix.buildType}}\qjs.exe tests\test_closure.js
build\${{matrix.buildType}}\qjs.exe tests\test_language.js
build\${{matrix.buildType}}\qjs.exe tests\test_builtin.js
Expand Down Expand Up @@ -267,6 +268,7 @@ jobs:
- name: test
run: |
build\qjs.exe tests\test_bigint.js
build\qjs.exe tests\test_bjson.js
build\qjs.exe tests\test_closure.js
build\qjs.exe tests\test_language.js
build\qjs.exe tests\test_builtin.js
Expand Down
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,6 @@ if(BUILD_EXAMPLES AND NOT WIN32)
if(APPLE)
target_link_options(point PRIVATE -undefined dynamic_lookup)
endif()

add_library(bjson MODULE tests/bjson.c)
set_target_properties(bjson PROPERTIES
PREFIX ""
C_VISIBILITY_PRESET default
)
target_compile_definitions(bjson PRIVATE JS_SHARED_LIBRARY)
if(APPLE)
target_link_options(bjson PRIVATE -undefined dynamic_lookup)
endif()
endif()

add_executable(test_fib
Expand Down
3 changes: 1 addition & 2 deletions doc/quickjs.texi
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,7 @@ Examples are available in @file{quickjs-libc.c}.
@subsection C Modules

Native ES6 modules are supported and can be dynamically or statically
linked. Look at the @file{test_bjson} and @file{bjson.so}
examples. The standard library @file{quickjs-libc.c} is also a good example
linked. The standard library @file{quickjs-libc.c} is a good example
of a native module.

@subsection Memory handling
Expand Down
5,626 changes: 2,815 additions & 2,811 deletions gen/repl.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static JSContext *JS_NewCustomContext(JSRuntime *rt)
/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
js_init_module_bjson(ctx, "bjson");

JSValue global = JS_GetGlobalObject(ctx);
JS_SetPropertyFunctionList(ctx, global, global_obj, countof(global_obj));
Expand Down
1 change: 1 addition & 0 deletions qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ int main(int argc, char **argv)
/* add system modules */
namelist_add(&cmodule_list, "std", "std", 0);
namelist_add(&cmodule_list, "os", "os", 0);
namelist_add(&cmodule_list, "bjson", "bjson", 0);

for(;;) {
c = getopt(argc, argv, "ho:N:mn:bxesvM:p:S:D:");
Expand Down
66 changes: 66 additions & 0 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4089,3 +4089,69 @@ void js_std_eval_binary(JSContext *ctx, const uint8_t *buf, size_t buf_len,
JS_FreeValue(ctx, val);
}
}

static JSValue js_bjson_read(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
uint8_t *buf;
uint64_t pos, len;
JSValue obj;
size_t size;
int flags;

if (JS_ToIndex(ctx, &pos, argv[1]))
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &len, argv[2]))
return JS_EXCEPTION;
buf = JS_GetArrayBuffer(ctx, &size, argv[0]);
if (!buf)
return JS_EXCEPTION;
if (pos + len > size)
return JS_ThrowRangeError(ctx, "array buffer overflow");
flags = 0;
if (JS_ToBool(ctx, argv[3]))
flags |= JS_READ_OBJ_REFERENCE;
Comment on lines +4111 to +4113
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow-up, assuming y'all think it's a good idea in the first place, I want to change this to an int and export the JS_READ_OBJ flags to JS land as module constants; ditto for writing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity: #479

obj = JS_ReadObject(ctx, buf + pos, len, flags);
return obj;
}

static JSValue js_bjson_write(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
size_t len;
uint8_t *buf;
JSValue array;
int flags;

flags = 0;
if (JS_ToBool(ctx, argv[1]))
flags |= JS_WRITE_OBJ_REFERENCE;
buf = JS_WriteObject(ctx, &len, argv[0], flags);
if (!buf)
return JS_EXCEPTION;
array = JS_NewArrayBufferCopy(ctx, buf, len);
js_free(ctx, buf);
return array;
}


static const JSCFunctionListEntry js_bjson_funcs[] = {
JS_CFUNC_DEF("read", 4, js_bjson_read ),
JS_CFUNC_DEF("write", 2, js_bjson_write ),
};

static int js_bjson_init(JSContext *ctx, JSModuleDef *m)
{
return JS_SetModuleExportList(ctx, m, js_bjson_funcs,
countof(js_bjson_funcs));
}

JSModuleDef *js_init_module_bjson(JSContext *ctx, const char *module_name)
{
JSModuleDef *m;
m = JS_NewCModule(ctx, module_name, js_bjson_init);
if (!m)
return NULL;
JS_AddModuleExportList(ctx, m, js_bjson_funcs, countof(js_bjson_funcs));
return m;
}
1 change: 1 addition & 0 deletions quickjs-libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern "C" {

JSModuleDef *js_init_module_std(JSContext *ctx, const char *module_name);
JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name);
JSModuleDef *js_init_module_bjson(JSContext *ctx, const char *module_name);
void js_std_add_helpers(JSContext *ctx, int argc, char **argv);
void js_std_loop(JSContext *ctx);
JSValue js_std_await(JSContext *ctx, JSValue obj);
Expand Down
4 changes: 3 additions & 1 deletion repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
*/
import * as std from "std";
import * as os from "os";
import * as bjson from "bjson";

(function(g) {
/* add 'os' and 'std' bindings */
/* add 'bjson', 'os' and 'std' bindings */
g.bjson = bjson;
g.os = os;
g.std = std;

Expand Down
96 changes: 0 additions & 96 deletions tests/bjson.c

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_bjson.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as bjson from "./bjson.so";
import * as bjson from "bjson";

function assert(actual, expected, message) {
if (arguments.length == 1)
Expand Down