Skip to content

Commit

Permalink
more wip, pulling inspect out of libc
Browse files Browse the repository at this point in the history
  • Loading branch information
suchipi committed Aug 12, 2024
1 parent e600ff5 commit bde4cc7
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 59 deletions.
5 changes: 5 additions & 0 deletions examples/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
print("print", 1, {});
console.log("console.log", 2, {});
console.warn("console.warn", 3, {});
console.error("console.error", 4, {});
console.info("console.info", 5, {});
2 changes: 2 additions & 0 deletions src/archives/core.ninja.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const deps_host = [
builddir("intermediate/quickjs-utils.host.o"),
builddir("intermediate/quickjs-modulesys.host.o"),
builddir("intermediate/quickjs-print.host.o"),
builddir("intermediate/quickjs-inspect.host.o"),
];

const deps_target = [
Expand All @@ -20,6 +21,7 @@ const deps_target = [
builddir("intermediate/quickjs-utils.target.o"),
builddir("intermediate/quickjs-modulesys.target.o"),
builddir("intermediate/quickjs-print.target.o"),
builddir("intermediate/quickjs-inspect.target.o"),
];

const core_host = build({
Expand Down
12 changes: 8 additions & 4 deletions src/archives/quickjs-full-init.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "quickjs-full-init.h"

extern const uint8_t qjsc_inspect[];
extern const uint32_t qjsc_inspect_size;

/* returns 0 on success, nonzero on failure */
static int quickjs_full_init_modules(JSContext *ctx)
{
Expand Down Expand Up @@ -33,7 +30,14 @@ static int quickjs_full_init_modules(JSContext *ctx)

static int quickjs_full_init_globals(JSContext *ctx)
{
return QJMS_EvalBinary(ctx, qjsc_inspect, qjsc_inspect_size, 0);
if (js_inspect_add_inspect_global(ctx)) {
return -1;
}

js_print_add_print_global(ctx);
js_print_add_console_global(ctx);

return 0;
}

int quickjs_full_init(JSContext *ctx)
Expand Down
2 changes: 2 additions & 0 deletions src/archives/quickjs-full-init.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define QUICKJS_FULL_INIT_H

#include "quickjs.h"
#include "quickjs-print.h"
#include "quickjs-inspect.h"
#include "quickjs-libc.h"
#include "quickjs-libbytecode.h"
#include "quickjs-libcontext.h"
Expand Down
26 changes: 0 additions & 26 deletions src/inspect/inspect.ninja.js

This file was deleted.

1 change: 1 addition & 0 deletions src/qjsbootstrap/qjsbootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ __static_yoink("blink_xnu_aarch64");
#include "execpath.h"
#include "cutils.h"
#include "quickjs-utils.h"
#include "quickjs-print.h"
#include "quickjs-modulesys.h"
#include "quickjs-full-init.h"

Expand Down
8 changes: 5 additions & 3 deletions src/qjsc/qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ __static_yoink("blink_xnu_aarch64");
#include "quickjs-modulesys.h"
#include "debugprint.h"

// TODO: clean this up
// Stub out inspect, intervals, and string-dedent, which we can't rely on in
// qjsc, because those are built with qjsc
const uint32_t qjsc_inspect_size = 0;
Expand Down Expand Up @@ -687,6 +688,7 @@ int main(int argc, char **argv)
if (output_type != OUTPUT_C) {
fprintf(fo, "#include \"quickjs-libc.h\"\n"
"#include \"quickjs-utils.h\"\n"
"#include \"quickjs-print.h\"\n"
"#include \"quickjs-modulesys.h\"\n"
"\n"
);
Expand Down Expand Up @@ -753,9 +755,7 @@ int main(int argc, char **argv)

fprintf(fo,
" {\n"
" extern const uint8_t qjsc_inspect[];\n"
" extern const uint32_t qjsc_inspect_size;\n"
" QJMS_EvalBinary(ctx, qjsc_inspect, qjsc_inspect_size, 0);\n"
" \n"
" }\n");

for(i = 0; i < cname_list.count; i++) {
Expand Down Expand Up @@ -784,6 +784,8 @@ int main(int argc, char **argv)
fprintf(fo,
" ctx = JS_NewCustomContext(rt);\n"
" js_std_add_helpers(ctx, argc, argv);\n"
" js_print_add_print_global(ctx);\n"
" js_print_add_console_global(ctx);\n"
" QJMS_InitContext(ctx);\n");

for(i = 0; i < cname_list.count; i++) {
Expand Down
10 changes: 5 additions & 5 deletions src/qjsc/qjsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ function main(args) {
case "programSource": {
out += '#include "quickjs-libc.h"\n';
out += '#include "quickjs-utils.h"\n';
out += '#include "quickjs-print.h"\n';
out += '#include "quickjs-inspect.h"\n';
out += '#include "quickjs-modulesys.h"\n';
out += "\n\n";

Expand Down Expand Up @@ -202,11 +204,7 @@ function main(args) {
out += " }\n";
}

out += " {\n";
out += " extern const uint8_t qjsc_inspect[];\n";
out += " extern const uint32_t qjsc_inspect_size;\n";
out += " QJMS_EvalBinary(ctx, qjsc_inspect, qjsc_inspect_size, 0);\n";
out += " }\n";
out += " js_inspect_add_inspect_global(ctx);\n";

out += " return ctx;\n";
out += "}\n\n";
Expand All @@ -227,6 +225,8 @@ function main(args) {
out += " QJMS_InitState(rt);\n";
out += " ctx = JS_NewCustomContext(rt);\n";
out += " js_std_add_helpers(ctx, argc, argv);\n";
out += " js_print_add_print_global(ctx);\n";
out += " js_print_add_console_global(ctx);\n";
out += " QJMS_InitContext(ctx);\n";

// This is where we actually load the compiled bytecode
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions src/quickjs-inspect/quickjs-inspect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "quickjs-inspect.h"
#include "quickjs-modulesys.h"

extern const uint8_t qjsc_inspect[];
extern const uint32_t qjsc_inspect_size;

int js_inspect_add_inspect_global(JSContext *ctx)
{
return QJMS_EvalBinary(ctx, qjsc_inspect, qjsc_inspect_size, 0);
}
File renamed without changes.
9 changes: 9 additions & 0 deletions src/quickjs-inspect/quickjs-inspect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef QUICKJS_INSPECT_H
#define QUICKJS_INSPECT_H

#include "quickjs.h"

/* Adds 'inspect' global */
int js_inspect_add_inspect_global(JSContext *ctx);

#endif /* ifndef QUICKJS_INSPECT_H */
41 changes: 41 additions & 0 deletions src/quickjs-inspect/quickjs-inspect.ninja.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// The C helper function for adding the global
build({
output: builddir("intermediate/quickjs-inspect.host.o"),
rule: "cc_host",
inputs: [rel("quickjs-inspect.c")],
});

build({
output: builddir("intermediate/quickjs-inspect.target.o"),
rule: "cc_target",
inputs: [rel("quickjs-inspect.c")],
});

// C bytecode file generated from js
const inspect_c = build({
output: builddir("intermediate/inspect.c"),
rule: "qjsc",
inputs: [rel("inspect.js")],
ruleVariables: {
qjsc_args: `-c -m`,
},
});

// The compiled objects containing the bytecode
build({
output: builddir("intermediate/inspect.host.o"),
rule: "cc_host",
inputs: [inspect_c],
});

build({
output: builddir("intermediate/inspect.target.o"),
rule: "cc_target",
inputs: [inspect_c],
});

build({
output: builddir("dts/quickjs-inspect.d.ts"),
rule: "copy",
inputs: [rel("quickjs-inspect.d.ts")],
});
21 changes: 6 additions & 15 deletions src/quickjs-libc/quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ sighandler_t signal(int signum, sighandler_t handler);
#include <stdatomic.h>
#endif

extern const uint8_t qjsc_inspect[];
extern const uint32_t qjsc_inspect_size;

extern const uint8_t qjsc_intervals[];
extern const uint32_t qjsc_intervals_size;

Expand All @@ -89,7 +86,6 @@ extern const uint32_t qjsc_string_dedent_size;
#include "quickjs-libc.h"
#include "quickjs-utils.h"
#include "quickjs-modulesys.h"
#include "quickjs-print.h"
#include "debugprint.h"
#include "execpath.h"

Expand Down Expand Up @@ -3914,6 +3910,12 @@ static void *worker_func(void *opaque)
JS_SetCanBlock(rt, TRUE);

js_std_add_helpers(ctx, -1, NULL);
// TODO: print and console should go here, but I want to untangle libc from
// the rest, so I don't want libc to depend on print right now
//
// js_print_add_print_global(ctx);
// js_print_add_console_global(ctx);
// js_inspect_add_inspect_global(ctx);
QJMS_InitContext(ctx);

if (!JS_RunModule(ctx, args->basename, args->filename))
Expand Down Expand Up @@ -4371,11 +4373,6 @@ JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name)

/**********************************************************/

void js_std_add_inspect(JSContext *ctx)
{
QJMS_EvalBinary(ctx, qjsc_inspect, qjsc_inspect_size, 0);
}

void js_std_add_scriptArgs(JSContext *ctx, int argc, char **argv)
{
JSValue global_obj, args;
Expand Down Expand Up @@ -4426,13 +4423,7 @@ void js_std_add_string_dedent(JSContext *ctx)
/**/
void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
{
js_std_add_inspect(ctx);
js_print_add_console_global(ctx);

/* scriptArgs and print are the same as in the mozilla JS shell */
js_print_add_print_global(ctx);
js_std_add_scriptArgs(ctx, argc, argv);

js_std_add_timeout(ctx);
js_std_add_intervals(ctx);
js_std_add_string_dedent(ctx);
Expand Down
6 changes: 1 addition & 5 deletions src/quickjs-libc/quickjs-libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ extern "C"
JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name);

/*
Creates 'inspect', 'console', 'print', 'scriptArgs', 'setTimeout',
'setInterval', and 'String.dedent'.
Creates 'scriptArgs', 'setTimeout', 'setInterval', and 'String.dedent'.
*/
void js_std_add_helpers(JSContext *ctx, int argc, char **argv);

/* Creates 'inspect' global */
void js_std_add_inspect(JSContext *ctx);

/* Creates 'scriptArgs' global */
void js_std_add_scriptArgs(JSContext *ctx, int argc, char **argv);

Expand Down
3 changes: 2 additions & 1 deletion src/quickjs-libcontext/quickjs-libcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "quickjs-libengine.h"
#include "quickjs-libcontext.h"
#include "quickjs-print.h"
#include "quickjs-inspect.h"

static JSClassID js_context_class_id;

Expand Down Expand Up @@ -260,7 +261,7 @@ static JSValue js_context_ctor(JSContext *ctx, JSValueConst this_val,
}

if (inspect) {
js_std_add_inspect(target_ctx);
js_inspect_add_inspect_global(target_ctx);
}
if (console) {
js_print_add_console_global(target_ctx);
Expand Down
31 changes: 31 additions & 0 deletions tests/inspect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { spawn } from "first-base";
import { binDir, rootDir } from "./_utils";

test("inspect", async () => {
const run = spawn(binDir("qjs"), [
"-e",
`
const obj = {
something: {
theSomething: true,
}
}
print(inspect(obj));
`,
]);
await run.completion;
expect(run.result).toMatchInlineSnapshot(`
{
"code": 0,
"error": false,
"stderr": "",
"stdout": "{
something: {
theSomething: true
}
}
",
}
`);
});
20 changes: 20 additions & 0 deletions tests/print.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { spawn } from "first-base";
import { binDir, rootDir } from "./_utils";

test("quickjs-print", async () => {
const run = spawn(binDir("qjs"), [rootDir("examples/logging.js")]);
await run.completion;
expect(run.result).toMatchInlineSnapshot(`
{
"code": 0,
"error": false,
"stderr": "console.warn 3 [object Object]
console.error 4 [object Object]
",
"stdout": "print 1 [object Object]
console.log 2 [object Object]
console.info 5 [object Object]
",
}
`);
});

0 comments on commit bde4cc7

Please sign in to comment.