dymokomi/luce-js
Source of release 0.6.0
#==============================================================================================
#
# ljs - Run JavaScript files with luce-js, like QuickJS's qjs
#
# DESCRIPTION:
# Port of qjs.c (QuickJS 2026-06-04): `ljs [options] [file [args]]` runs a script or
# a module, `-e EXPR` evaluates an expression, and without a file (or with -i) ljs
# runs QuickJS's REPL, repl.js, whose bytecode tests/repl.lucb holds as qjs links
# repl.c (both are generated by the compiler: `cd tests && ../build/ljsc -s -c -o
# repl.lucb -m repl.js`, as the Makefile runs qjsc; tests/tools.py checks that it is
# current). Every option of qjs is ported, with the same short and long spellings,
# combined short options (-qd) and exit statuses: -h, -e, -i, -m, --script, --strict,
# -I, --std, -T (trace the allocations), -d (dump the memory usage, and with -q the
# instantiation times: `ljs -qd` is `make stats`), --memory-limit, --stack-size,
# --no-unhandled-rejection, -s, --strip-source and -q.
#
# Shared-library modules are the native modules ljs links in and registers: "bjson"
# (tests/bjson.c) and the examples "fib" and "point", imported as "./bjson.so" or
# "bjson" and so on.
#
# Differences from the C: the program is named ljs in its messages; the default stack
# size is luce-js's (js_default_stack_size).
#
# Build: luce-base build tests/ljs.lucb -o build/ljs
#
# QuickJS stand alone interpreter
#
# Copyright (c) 2017-2021 Fabrice Bellard
# Copyright (c) 2017-2021 Charlie Gordon
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the following
# conditions: the above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS
# IS", WITHOUT WARRANTY OF ANY KIND.
#
#==============================================================================================
import c
import platform
from luce_js import host
from luce_js import engine as js
import repl
import bjson
from luce_js_examples import fib, point
extern func malloc(size: usize) -> void*?
extern func realloc(ptr: void*?, size: usize) -> void*?
extern func c_free as "free"(ptr: void*?)
extern func malloc_size(ptr: const void*?) -> usize
extern func malloc_usable_size(ptr: const void*?) -> usize
extern func msize as "_msize"(ptr: const void*?) -> usize
extern func strlen(s: const u8*) -> usize
extern func strcmp(a: const u8*, b: const u8*) -> i32
extern func strtod(s: const u8*, endp: (u8*)*?) -> f64
extern func printf(cformat: const u8*, ...) -> i32
extern func fprintf(f: void*, cformat: const u8*, ...) -> i32
extern func putchar(ch: i32) -> i32
extern func fflush(f: void*?) -> i32
extern func perror(text: const u8*)
extern func clock() -> u64
extern func c_exit as "exit"(status: i32) -> never
## The NUL-terminated text of a literal.
func z(text: str) -> const u8*:
return text.bytes.data
func stderr() -> void*:
return c.stderr()
# mark: Evaluation ==============================================================================
func eval_buf(ctx: js.Context*, buf: const u8*, buf_len: usize, filename: const u8*, eval_flags: i32) -> i32:
var failed = false
var val = js.js_undefined()
if (eval_flags & js.eval_type_mask) == js.eval_type_module:
# for the modules, we compile then run to be able to set import.meta
let compiled = js.js_eval(ctx, buf, buf_len, filename, eval_flags | js.eval_flag_compile_only) catch:
failed = true
recover js.js_undefined()
if not failed:
# as in qjs, a failure here (realpath() of "<input>") is ignored
host.js_module_set_import_meta(ctx, compiled, true, true) catch: recover ()
let promise = js.js_eval_function(ctx, compiled) catch:
failed = true
recover js.js_undefined()
if not failed:
val = host.js_std_await(ctx, promise) catch:
failed = true
recover js.js_undefined()
else:
val = js.js_eval(ctx, buf, buf_len, filename, eval_flags) catch:
failed = true
recover js.js_undefined()
if failed:
host.js_std_dump_error(ctx)
return -1
js.js_free_value(ctx, val)
return 0
## Whether the NUL-terminated `name` ends with `suffix` (has_suffix).
func has_suffix(name: const u8*, suffix: str) -> bool:
let len = strlen(name)
return len >= suffix.length and (str)u8[]((u8*)name + (len - suffix.length), suffix.length) == suffix
## module: 1 module, 0 script, -1 detect from the name and the source.
func eval_file(ctx: js.Context*, filename: const u8*, module: i32, strict: bool) -> i32:
var buf_len: usize = 0
let loaded = host.js_load_file(ctx, &buf_len, filename)
if loaded == none:
perror(filename)
let buf = loaded else c_exit(1)
var is_module = module > 0
if module < 0:
is_module = has_suffix(filename, ".mjs") or js.js_detect_module(buf, buf_len)
var eval_flags = js.eval_type_global
if is_module:
eval_flags = js.eval_type_module
elif strict:
eval_flags |= js.eval_flag_strict
let ret = eval_buf(ctx, buf, buf_len, filename, eval_flags)
js.js_free(ctx, buf)
return ret
## also used to initialize the worker context
func js_new_custom_context(rt: js.Runtime*) -> js.Context*?:
let ctx = js.js_new_context(rt) else return none
# system modules
_ = host.js_init_module_std(ctx, z("std")) catch: return ctx
_ = host.js_init_module_os(ctx, z("os")) catch: return ctx
return ctx
# mark: Allocation tracing ======================================================================
## the bytes the C library keeps per block beside the usable size
let malloc_overhead: usize = 0 if platform.macos else 8
## The base address the traced pointers are printed relative to (struct trace_malloc_data).
struct TraceMallocData:
var base: u8*?
func js_trace_malloc_ptr_offset(ptr: u8*, dp: TraceMallocData*) -> i64:
let base = dp.base else return (i64)(usize)ptr
return (i64)((isize)(usize)ptr - (isize)(usize)base)
## default memory allocation functions with memory limitation
func js_trace_malloc_usable_size(ptr: const void*?) -> usize:
if platform.macos:
return malloc_size(ptr)
elif platform.windows:
return msize(ptr)
elif platform.wasm32:
return 0
else:
return malloc_usable_size(ptr)
## The "%p" of js_trace_malloc_printf: NULL, or H<offset from the base>.<usable size>.
func trace_ptr(s: js.MallocState*, ptr: void*?):
if ptr == none:
_ = printf(z("NULL"))
return
let p = ptr else return
let dp = (TraceMallocData*)(s.opaque else trap("js_trace_malloc: no trace data"))
_ = printf(z("H%+06lld.%zd"), js_trace_malloc_ptr_offset((u8*)p, dp), js_trace_malloc_usable_size(p))
func js_trace_malloc_init(s: TraceMallocData*):
let base = malloc(8)
s.base = (u8*?)base
c_free(base)
func js_trace_malloc(s: js.MallocState*, size: usize) -> void*?:
# Do not allocate zero bytes: behavior is platform dependent
assert(size != 0)
if s.malloc_size + size > s.malloc_limit:
return none
let ptr = malloc(size)
_ = printf(z("A %zd -> "), size)
trace_ptr(s, ptr)
_ = printf(z("\n"))
if let p = ptr:
s.malloc_count += 1
s.malloc_size += js_trace_malloc_usable_size(p) + malloc_overhead
return ptr
func js_trace_free(s: js.MallocState*, ptr: void*?):
let p = ptr else return
_ = printf(z("F "))
trace_ptr(s, p)
_ = printf(z("\n"))
s.malloc_count -= 1
s.malloc_size -= js_trace_malloc_usable_size(p) + malloc_overhead
c_free(p)
func js_trace_realloc(s: js.MallocState*, ptr: void*?, size: usize) -> void*?:
if ptr == none:
if size == 0:
return none
return js_trace_malloc(s, size)
let old = ptr else return none
let old_size = js_trace_malloc_usable_size(old)
if size == 0:
_ = printf(z("R %zd "), size)
trace_ptr(s, old)
_ = printf(z("\n"))
s.malloc_count -= 1
s.malloc_size -= old_size + malloc_overhead
c_free(old)
return none
if s.malloc_size + size - old_size > s.malloc_limit:
return none
_ = printf(z("R %zd "), size)
trace_ptr(s, old)
let result = realloc(old, size)
_ = printf(z(" -> "))
trace_ptr(s, result)
_ = printf(z("\n"))
if let p = result:
s.malloc_size += js_trace_malloc_usable_size(p) - old_size
return result
let trace_mf = js.MallocFunctions(
js_malloc = js_trace_malloc,
js_free = js_trace_free,
js_realloc = js_trace_realloc,
js_malloc_usable_size = js_trace_malloc_usable_size,
)
# mark: Options =================================================================================
## A size with an optional G, M or k suffix (get_suffixed_size).
func get_suffixed_size(text: const u8*) -> usize:
var p: u8* = ---
var v = (usize)strtod(text, &p)
match p[0]:
'G':
v <<= 30
'M':
v <<= 20
'k', 'K':
v <<= 10
_:
if p[0] != 0:
_ = fprintf(stderr(), z("ljs: invalid suffix: %s\n"), p)
c_exit(1)
return v
let prog_name: str = "ljs"
## The QuickJS version luce-js ports (CONFIG_VERSION).
let config_version: str = "2026-06-04"
func help() -> never:
_ = printf(z("luce-js (QuickJS version %s)\nusage: %s [options] [file [args]]\n-h --help list options\n-e --eval EXPR evaluate EXPR\n-i --interactive go to interactive mode\n-m --module load as ES6 module (default=autodetect)\n --script load as ES6 script (default=autodetect)\n --strict force strict mode\n-I --include file include an additional file\n --std make 'std' and 'os' available to the loaded script\n-T --trace trace memory allocation\n-d --dump dump the memory usage stats\n --memory-limit n limit the memory usage to 'n' bytes (SI suffixes allowed)\n --stack-size n limit the stack size to 'n' bytes (SI suffixes allowed)\n --no-unhandled-rejection ignore unhandled promise rejections\n-s strip all the debug info\n --strip-source strip the source code\n-q --quit just instantiate the interpreter and quit\n"),
config_version.bytes.data, z(prog_name))
c_exit(1)
## The command line of ljs, as qjs's main keeps it in locals.
struct Options:
var expr: const u8*?
var interactive: i32
var dump_memory: i32
var trace_memory: i32
var empty_run: i32
var module: i32
var strict: bool
var load_std: bool
var dump_unhandled_promise_rejection: bool
var memory_limit: usize
var include_list: (const u8*)[32]
var include_count: usize
var strip_flags: i32
var stack_size: usize
## Parse the options of `argv`; answer the index of the first argument that is not one.
## (cannot use getopt because we want to pass the command line to the script)
func parse_options(o: Options*, argv: str[]) -> usize:
var optind: usize = 1
while optind < argv.length and argv[optind].length > 0 and argv[optind].bytes[0] == '-':
var arg: const u8* = argv[optind].bytes.data + 1
var longopt: const u8* = z("")
# a single - is not an option, it also stops argument scanning
if arg[0] == 0:
break
optind += 1
if arg[0] == '-':
longopt = arg + 1
arg = arg + strlen(arg)
# -- stops argument scanning
if longopt[0] == 0:
break
while arg[0] != 0 or longopt[0] != 0:
let opt = arg[0]
if opt != 0:
arg = arg + 1
let long_name = (str)u8[]((u8*)longopt, strlen(longopt))
longopt = z("")
if opt == 'h' or opt == '?' or long_name == "help":
help()
if opt == 'e' or long_name == "eval":
if arg[0] != 0:
o.expr = arg
break
if optind < argv.length:
o.expr = argv[optind].bytes.data
optind += 1
break
_ = fprintf(stderr(), z("ljs: missing expression for -e\n"))
c_exit(2)
if opt == 'I' or long_name == "include":
if optind >= argv.length:
_ = fprintf(stderr(), z("expecting filename"))
c_exit(1)
if o.include_count >= 32:
_ = fprintf(stderr(), z("too many included files"))
c_exit(1)
o.include_list[o.include_count] = argv[optind].bytes.data
o.include_count += 1
optind += 1
continue
if opt == 'i' or long_name == "interactive":
o.interactive += 1
continue
if opt == 'm' or long_name == "module":
o.module = 1
continue
if long_name == "script":
o.module = 0
continue
if long_name == "strict":
o.strict = true
continue
if opt == 'd' or long_name == "dump":
o.dump_memory += 1
continue
if opt == 'T' or long_name == "trace":
o.trace_memory += 1
continue
if long_name == "std":
o.load_std = true
continue
if long_name == "no-unhandled-rejection":
o.dump_unhandled_promise_rejection = false
continue
if opt == 'q' or long_name == "quit":
o.empty_run += 1
continue
if long_name == "memory-limit":
if optind >= argv.length:
_ = fprintf(stderr(), z("expecting memory limit"))
c_exit(1)
o.memory_limit = get_suffixed_size(argv[optind].bytes.data)
optind += 1
continue
if long_name == "stack-size":
if optind >= argv.length:
_ = fprintf(stderr(), z("expecting stack size"))
c_exit(1)
o.stack_size = get_suffixed_size(argv[optind].bytes.data)
optind += 1
continue
if opt == 's':
o.strip_flags = js.strip_debug
continue
if long_name == "strip-source":
o.strip_flags = js.strip_source
continue
if opt != 0:
_ = fprintf(stderr(), z("ljs: unknown option '-%c'\n"), (i32)opt)
else:
_ = fprintf(stderr(), z("ljs: unknown option '--%.*s'\n"), (i32)long_name.length,
long_name.bytes.data)
help()
return optind
# mark: main ====================================================================================
pub func main(argv: str[]) -> i32:
var trace_data = TraceMallocData(base = none)
var o: Options = ---
o.expr = none
o.interactive = 0
o.dump_memory = 0
o.trace_memory = 0
o.empty_run = 0
o.module = -1
o.strict = false
o.load_std = false
o.dump_unhandled_promise_rejection = true
o.memory_limit = 0
o.include_count = 0
o.strip_flags = 0
o.stack_size = 0
let optind = parse_options(&o, argv)
# the native modules qjs would load as shared libraries
_ = host.js_register_native_module("bjson", bjson.js_init_module_bjson)
_ = host.js_register_native_module("fib", fib.js_init_module_fib)
_ = host.js_register_native_module("point", point.js_init_module)
var created: js.Runtime*? = none
if o.trace_memory != 0:
js_trace_malloc_init(&trace_data)
created = js.js_new_runtime2(&trace_mf, &trace_data)
else:
created = js.js_new_runtime()
if created == none:
_ = fprintf(stderr(), z("ljs: cannot allocate JS runtime\n"))
let rt = created else c_exit(2)
if o.memory_limit != 0:
js.js_set_memory_limit(rt, o.memory_limit)
if o.stack_size != 0:
js.js_set_max_stack_size(rt, o.stack_size)
js.js_set_strip_info(rt, o.strip_flags)
host.js_std_set_worker_new_context_func(js_new_custom_context)
host.js_std_init_handlers(rt)
let new_ctx = js_new_custom_context(rt)
if new_ctx == none:
_ = fprintf(stderr(), z("ljs: cannot allocate JS context\n"))
let ctx = new_ctx else c_exit(2)
# loader for ES6 modules
js.js_set_module_loader_func2(rt, none, host.js_module_loader, host.js_module_check_attributes, none)
if o.dump_unhandled_promise_rejection:
js.js_set_host_promise_rejection_tracker(rt, host.js_std_promise_rejection_tracker, none)
if o.empty_run == 0:
if run(ctx, rt, argv, optind, &o) != 0:
host.js_std_free_handlers(rt)
js.js_free_context(ctx)
js.js_free_runtime(rt)
return 1
if o.dump_memory != 0:
var stats: js.MemoryUsage = ---
js.js_compute_memory_usage(rt, &stats)
# JS_DumpMemoryUsage writes with Luce's print: flush what C stdio holds first
_ = fflush(c.stdout())
js.js_dump_memory_usage(&stats, rt)
host.js_std_free_handlers(rt)
js.js_free_context(ctx)
js.js_free_runtime(rt)
if o.empty_run != 0 and o.dump_memory != 0:
instantiation_times()
return 0
## Everything qjs does between creating the context and the memory dump; 1 on a failure.
func run(ctx: js.Context*, rt: js.Runtime*, argv: str[], optind: usize, o: Options*) -> i32:
host.js_std_add_helpers(ctx, argv[optind..]) catch:
host.js_std_dump_error(ctx)
return 1
# make 'std' and 'os' visible to non module code
if o.load_std:
let text = "import * as std from 'std';\nimport * as os from 'os';\nglobalThis.std = std;\nglobalThis.os = os;\n"
_ = eval_buf(ctx, text.bytes.data, text.length, z("<input>"), js.eval_type_module)
for i in 0..<o.include_count:
if eval_file(ctx, o.include_list[i], 0, o.strict) != 0:
return 1
var interactive = o.interactive
if let expr = o.expr:
var eval_flags = js.eval_type_global
if o.module > 0:
eval_flags = js.eval_type_module
elif o.strict:
eval_flags |= js.eval_flag_strict
if eval_buf(ctx, expr, strlen(expr), z("<cmdline>"), eval_flags) != 0:
return 1
elif optind >= argv.length:
# interactive mode
interactive = 1
else:
let filename = argv[optind].bytes.data
if eval_file(ctx, filename, o.module, o.strict) != 0:
return 1
if interactive != 0:
js.js_set_host_promise_rejection_tracker(rt, none, none)
host.js_std_eval_binary(ctx, &repl.qjsc_repl[0], repl.qjsc_repl_size, false)
host.js_std_loop(ctx)
return 0
## CLOCKS_PER_SEC (POSIX requires one million).
let clocks_per_sec: f64 = 1000000.0
## The best of 100 times to create and free a runtime and a context (`ljs -qd`).
func instantiation_times():
var t: u64[5] = ---
var best: f64[5] = ---
for i in 0..<100:
t[0] = clock()
let rt = js.js_new_runtime() else trap("cannot allocate JS runtime")
t[1] = clock()
let ctx = js.js_new_context(rt) else trap("cannot allocate JS context")
t[2] = clock()
js.js_free_context(ctx)
t[3] = clock()
js.js_free_runtime(rt)
t[4] = clock()
var j = 4
while j > 0:
let ms = 1000.0 * f64(t[j] - t[j - 1]) / clocks_per_sec
if i == 0 or best[j] > ms:
best[j] = ms
j -= 1
_ = printf(z("\nInstantiation times (ms): %.3f = %.3f+%.3f+%.3f+%.3f\n"),
best[1] + best[2] + best[3] + best[4], best[1], best[2], best[3], best[4])