Compare commits
48 Commits
024d796ca4
...
mach
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7469383e66 | ||
|
|
1fee8f9f8b | ||
|
|
d18ea1b330 | ||
|
|
4de0659474 | ||
|
|
27a9b72b07 | ||
|
|
a3622bd5bd | ||
|
|
2f6700415e | ||
|
|
243d92f7f3 | ||
|
|
8f9d026b9b | ||
|
|
2c9ac8f7b6 | ||
|
|
80f24e131f | ||
|
|
a8f8af7662 | ||
|
|
f5b3494762 | ||
|
|
13a6f6c79d | ||
|
|
1a925371d3 | ||
|
|
08d2bacb1f | ||
|
|
7322153e57 | ||
|
|
cc72c4cb0f | ||
|
|
ae1f09a28f | ||
|
|
3c842912a1 | ||
|
|
7cacf32078 | ||
|
|
b740612761 | ||
|
|
6001c2b4bb | ||
|
|
98625fa15b | ||
|
|
87fafa44c8 | ||
|
|
45ce76aef7 | ||
|
|
32fb44857c | ||
|
|
31d67f6710 | ||
|
|
3621b1ef33 | ||
|
|
836227c8d3 | ||
|
|
0ae59705d4 | ||
|
|
8e2607b6ca | ||
|
|
dc73e86d8c | ||
|
|
555cceb9d6 | ||
|
|
fbb7933eb6 | ||
|
|
0287d6ada4 | ||
|
|
73cd6a255d | ||
|
|
83ea67c01b | ||
|
|
16059cca4e | ||
|
|
9ffe60ebef | ||
|
|
2beafec5d9 | ||
|
|
aba8eb66bd | ||
|
|
1abcaa92c7 | ||
|
|
168f7c71d5 | ||
|
|
56ed895b6e | ||
|
|
1e4646999d | ||
|
|
68d6c907fe | ||
|
|
8150c64c7d |
@@ -382,13 +382,13 @@ static const JSCFunctionListEntry js_reader_funcs[] = {
|
|||||||
JSValue js_miniz_use(JSContext *js)
|
JSValue js_miniz_use(JSContext *js)
|
||||||
{
|
{
|
||||||
JS_NewClassID(&js_reader_class_id);
|
JS_NewClassID(&js_reader_class_id);
|
||||||
JS_NewClass(JS_GetRuntime(js), js_reader_class_id, &js_reader_class);
|
JS_NewClass(js, js_reader_class_id, &js_reader_class);
|
||||||
JSValue reader_proto = JS_NewObject(js);
|
JSValue reader_proto = JS_NewObject(js);
|
||||||
JS_SetPropertyFunctionList(js, reader_proto, js_reader_funcs, sizeof(js_reader_funcs) / sizeof(JSCFunctionListEntry));
|
JS_SetPropertyFunctionList(js, reader_proto, js_reader_funcs, sizeof(js_reader_funcs) / sizeof(JSCFunctionListEntry));
|
||||||
JS_SetClassProto(js, js_reader_class_id, reader_proto);
|
JS_SetClassProto(js, js_reader_class_id, reader_proto);
|
||||||
|
|
||||||
JS_NewClassID(&js_writer_class_id);
|
JS_NewClassID(&js_writer_class_id);
|
||||||
JS_NewClass(JS_GetRuntime(js), js_writer_class_id, &js_writer_class);
|
JS_NewClass(js, js_writer_class_id, &js_writer_class);
|
||||||
JSValue writer_proto = JS_NewObject(js);
|
JSValue writer_proto = JS_NewObject(js);
|
||||||
JS_SetPropertyFunctionList(js, writer_proto, js_writer_funcs, sizeof(js_writer_funcs) / sizeof(JSCFunctionListEntry));
|
JS_SetPropertyFunctionList(js, writer_proto, js_writer_funcs, sizeof(js_writer_funcs) / sizeof(JSCFunctionListEntry));
|
||||||
JS_SetClassProto(js, js_writer_class_id, writer_proto);
|
JS_SetClassProto(js, js_writer_class_id, writer_proto);
|
||||||
|
|||||||
287
docs/pitcode.md
Normal file
287
docs/pitcode.md
Normal file
@@ -0,0 +1,287 @@
|
|||||||
|
# Pitmachine
|
||||||
|
A pitmachine is an abstract register machine for executing pitcode.
|
||||||
|
|
||||||
|
The pitmachine assembly language is JSON. The instructions field contains an array of labels and instructions. Labels are the targets of branch instructions. They are simple text. Slots are elements in an activation frame. They are designated by a small positive integer. Slots are the general registers of the pitmachine. Slots hold the arguments, variables, and temporaries of a function invocation.
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "program_name",
|
||||||
|
"data": {⸳⸳⸳},
|
||||||
|
"source": "...",
|
||||||
|
The labels record associates labels with tokens for use by debuggers.
|
||||||
|
|
||||||
|
"labels": {
|
||||||
|
"entry": token,
|
||||||
|
"beyond": token,
|
||||||
|
...
|
||||||
|
},
|
||||||
|
The instructions array is a list of instructions and labels.
|
||||||
|
|
||||||
|
instructions: [
|
||||||
|
A statement label:
|
||||||
|
|
||||||
|
"entry",
|
||||||
|
go to beyond:
|
||||||
|
|
||||||
|
["jump", "beyond"],
|
||||||
|
assign slot 8: pi / 2
|
||||||
|
|
||||||
|
["access", 13, {"kind": "name", "name": "pi", "make": "intrinsic", ⸳⸳⸳}],
|
||||||
|
["int", 14, 2],
|
||||||
|
["divide", 8, 13, 14],
|
||||||
|
⸳⸳⸳
|
||||||
|
"beyond"
|
||||||
|
⸳⸳⸳
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
## Pitcode instructions
|
||||||
|
This is a register based machine.
|
||||||
|
|
||||||
|
### Arithmetic
|
||||||
|
Arithmetic instructions perform operations on numbers. All other cases disrupt (except add, which is polymorphic).
|
||||||
|
|
||||||
|
"add", dest, left, right // dest, left, and right are numbers designating slots in the current activation frame. Also works on texts (concatenation).
|
||||||
|
|
||||||
|
"subtract", dest, left, right
|
||||||
|
|
||||||
|
"multiply", dest, left, right
|
||||||
|
|
||||||
|
"divide", dest, left, right
|
||||||
|
|
||||||
|
"integer_divide", dest, left, right
|
||||||
|
|
||||||
|
"modulo", dest, left, right
|
||||||
|
|
||||||
|
"remainder", dest, left, right
|
||||||
|
|
||||||
|
"max", dest, left, right
|
||||||
|
|
||||||
|
"min", dest, left, right
|
||||||
|
|
||||||
|
"abs", dest, right
|
||||||
|
|
||||||
|
"neg", dest, right
|
||||||
|
|
||||||
|
"sign", dest, right
|
||||||
|
|
||||||
|
"fraction", dest, right
|
||||||
|
|
||||||
|
"integer", dest, right
|
||||||
|
|
||||||
|
"ceiling", dest, right, place
|
||||||
|
|
||||||
|
"floor", dest, right, place
|
||||||
|
|
||||||
|
"round", dest, right, place
|
||||||
|
|
||||||
|
"trunc", dest, right, place
|
||||||
|
|
||||||
|
### Text
|
||||||
|
Text instructions perform operations on texts. All other cases disrupt.
|
||||||
|
|
||||||
|
"concat", dest, left, right // concat two texts
|
||||||
|
|
||||||
|
"concat_space", dest, left, right
|
||||||
|
|
||||||
|
"character", dest, right
|
||||||
|
|
||||||
|
"codepoint", dest, right
|
||||||
|
|
||||||
|
"length", dest, right
|
||||||
|
|
||||||
|
"lower", dest, right
|
||||||
|
|
||||||
|
"upper", dest, right
|
||||||
|
|
||||||
|
"append", pretext, right
|
||||||
|
|
||||||
|
Append the right text to the pretext, forwarding and growing its capacity if necessary.
|
||||||
|
|
||||||
|
### Comparison
|
||||||
|
Comparison instructions perform operations on texts or numbers. All other cases disrupt.
|
||||||
|
|
||||||
|
"eq", dest, left, right
|
||||||
|
|
||||||
|
"ne", dest, left, right
|
||||||
|
|
||||||
|
"lt", dest, left, right
|
||||||
|
|
||||||
|
"le", dest, left, right
|
||||||
|
|
||||||
|
"gt", dest, left, right
|
||||||
|
|
||||||
|
"ge", dest, left, right
|
||||||
|
|
||||||
|
### Logical
|
||||||
|
|
||||||
|
"not", dest, right
|
||||||
|
|
||||||
|
### Bitwise
|
||||||
|
Bitwise instructions convert operands to 32-bit integers. Non-numbers disrupt.
|
||||||
|
|
||||||
|
"bitand", dest, left, right
|
||||||
|
|
||||||
|
"bitor", dest, left, right
|
||||||
|
|
||||||
|
"bitxor", dest, left, right
|
||||||
|
|
||||||
|
"bitnot", dest, right
|
||||||
|
|
||||||
|
"shl", dest, left, right
|
||||||
|
|
||||||
|
"shr", dest, left, right
|
||||||
|
|
||||||
|
"ushr", dest, left, right
|
||||||
|
|
||||||
|
### Function
|
||||||
|
|
||||||
|
"frame", dest, func, nr_args
|
||||||
|
|
||||||
|
Prepare to invoke the func object. If the nr_args is too large, disrupt. Allocate the new activation frame. Put the current frame pointer into it.
|
||||||
|
|
||||||
|
"goframe", dest, func, nr_args
|
||||||
|
|
||||||
|
Same as frame, except that the current frame is reused if it is large enough.
|
||||||
|
|
||||||
|
"invoke", frame
|
||||||
|
|
||||||
|
Store the next instruction address in the current frame. Make the new frame the current frame. Jump to the entry point.
|
||||||
|
|
||||||
|
"goinvoke", frame
|
||||||
|
|
||||||
|
"apply", func, array
|
||||||
|
|
||||||
|
"return", value
|
||||||
|
|
||||||
|
"return_value", dest
|
||||||
|
|
||||||
|
"setarg", frame, slot, value // set the slot of frame to value
|
||||||
|
|
||||||
|
### Branching
|
||||||
|
|
||||||
|
"jump", label
|
||||||
|
|
||||||
|
"jump_true", slot, label
|
||||||
|
|
||||||
|
If the value in the slot is true, jump to the label. Otherwise, continue with the next instruction.
|
||||||
|
|
||||||
|
"jump_false": slot, label
|
||||||
|
|
||||||
|
If the value in the slot is false, jump to the label. Otherwise, continue with the next instruction.
|
||||||
|
|
||||||
|
"jump_null": slot, label
|
||||||
|
|
||||||
|
"jump_empty": slot, label
|
||||||
|
|
||||||
|
"wary_true", slot, label
|
||||||
|
|
||||||
|
If the value in the slot is true, jump to the label. If the value is false, continue with the next instruction. Otherwise disrupt because of a type error.
|
||||||
|
|
||||||
|
"wary_false": slot, label
|
||||||
|
|
||||||
|
If the value in the slot is false, jump to the label. If the value is true, continue with the next instruction. Otherwise disrupt because of a type error.
|
||||||
|
|
||||||
|
### Sensory
|
||||||
|
|
||||||
|
Does the right slot contain a value of the indicated type?
|
||||||
|
|
||||||
|
"array?", dest, right
|
||||||
|
|
||||||
|
"blob?", dest, right
|
||||||
|
|
||||||
|
"character?", dest, right
|
||||||
|
|
||||||
|
"data?", dest, right
|
||||||
|
|
||||||
|
"digit?", dest, right
|
||||||
|
|
||||||
|
"false?", dest, right
|
||||||
|
|
||||||
|
"fit?", dest, right
|
||||||
|
|
||||||
|
"function?", dest, right
|
||||||
|
|
||||||
|
"integer?", dest, right
|
||||||
|
|
||||||
|
"letter?", dest, right
|
||||||
|
|
||||||
|
"logical?", dest, right
|
||||||
|
|
||||||
|
"null?", dest, right
|
||||||
|
|
||||||
|
"pattern?", dest, right
|
||||||
|
|
||||||
|
"record?", dest, right
|
||||||
|
|
||||||
|
"stone?", dest, right
|
||||||
|
|
||||||
|
"text?", dest, right
|
||||||
|
|
||||||
|
"true?", dest, right
|
||||||
|
|
||||||
|
"upper?", dest, right
|
||||||
|
|
||||||
|
"whitespace?", dest, right
|
||||||
|
|
||||||
|
### Potpourri
|
||||||
|
|
||||||
|
"stone", dest, right // stone an object
|
||||||
|
|
||||||
|
"true", dest
|
||||||
|
|
||||||
|
"false", dest
|
||||||
|
|
||||||
|
"null", dest
|
||||||
|
|
||||||
|
"move", dest, right
|
||||||
|
|
||||||
|
"int", dest, small_int
|
||||||
|
|
||||||
|
"access", dest, literal
|
||||||
|
|
||||||
|
This is used to access values (numbers, texts) from the program's immutable memory. The literal is a number or text.
|
||||||
|
|
||||||
|
"load", dest, object, subscript
|
||||||
|
|
||||||
|
This is used to load values from records and arrays.
|
||||||
|
|
||||||
|
"store", dest, object, subscript
|
||||||
|
|
||||||
|
This is used to store values into records and arrays.
|
||||||
|
|
||||||
|
"delete", object, subscript
|
||||||
|
|
||||||
|
used to delete a field from a record.
|
||||||
|
|
||||||
|
"get", dest, slot, level
|
||||||
|
|
||||||
|
This is used to get values from slots in outer frames.
|
||||||
|
|
||||||
|
"put", value, slot, level
|
||||||
|
|
||||||
|
This is used to store values into slots in outer frames.
|
||||||
|
|
||||||
|
"push", array, slot
|
||||||
|
|
||||||
|
Append to a mutable array.
|
||||||
|
|
||||||
|
"pop", dest, array
|
||||||
|
|
||||||
|
Remove the last element of a mutable array, putting the element into dest.
|
||||||
|
|
||||||
|
"disrupt"
|
||||||
|
|
||||||
|
### Make
|
||||||
|
|
||||||
|
"array", dest, nr_elements
|
||||||
|
|
||||||
|
"blob", dest, nr_bits
|
||||||
|
|
||||||
|
"function", dest, code_name_text
|
||||||
|
|
||||||
|
"pretext", dest, nr_characters
|
||||||
|
|
||||||
|
A pretext must be converted to text by stone before it can leave the function scope.
|
||||||
|
|
||||||
|
"record", dest, nr_elements
|
||||||
|
|
||||||
@@ -1,17 +1,21 @@
|
|||||||
(function engine() {
|
|
||||||
// Hidden vars (os, actorsym, init, core_path) come from env
|
// Hidden vars (os, actorsym, init, core_path) come from env
|
||||||
var ACTORDATA = actorsym
|
var ACTORDATA = actorsym
|
||||||
var SYSYM = '__SYSTEM__'
|
var SYSYM = '__SYSTEM__'
|
||||||
|
|
||||||
var _cell = {}
|
var _cell = {}
|
||||||
|
var need_stop = false
|
||||||
|
|
||||||
var dylib_ext
|
var dylib_ext
|
||||||
|
|
||||||
switch(os.platform()) {
|
var cases = {
|
||||||
case 'Windows': dylib_ext = '.dll'; break;
|
Windows: '.dll',
|
||||||
case 'macOS': dylib_ext = '.dylib'; break;
|
macOS: '.dylib',
|
||||||
case 'Linux': dylib_ext = '.so'; break;
|
Linux: '.so'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print(os.platform())
|
||||||
|
|
||||||
|
dylib_ext = cases[os.platform()]
|
||||||
|
|
||||||
var MOD_EXT = '.cm'
|
var MOD_EXT = '.cm'
|
||||||
var ACTOR_EXT = '.ce'
|
var ACTOR_EXT = '.ce'
|
||||||
@@ -51,14 +55,16 @@ var fd = use_embed('fd')
|
|||||||
// Get the shop path from HOME environment
|
// Get the shop path from HOME environment
|
||||||
var home = os.getenv('HOME') || os.getenv('USERPROFILE')
|
var home = os.getenv('HOME') || os.getenv('USERPROFILE')
|
||||||
if (!home) {
|
if (!home) {
|
||||||
throw Error('Could not determine home directory')
|
os.print('Could not determine home directory\n')
|
||||||
|
os.exit(1)
|
||||||
}
|
}
|
||||||
var shop_path = home + '/.cell'
|
var shop_path = home + '/.cell'
|
||||||
var packages_path = shop_path + '/packages'
|
var packages_path = shop_path + '/packages'
|
||||||
var core_path = packages_path + '/core'
|
var core_path = packages_path + '/core'
|
||||||
|
|
||||||
if (!fd.is_dir(core_path)) {
|
if (!fd.is_dir(core_path)) {
|
||||||
throw Error('Cell shop not found at ' + shop_path + '. Run "cell install" to set up.')
|
os.print('Cell shop not found at ' + shop_path + '. Run "cell install" to set up.\n')
|
||||||
|
os.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var use_cache = {}
|
var use_cache = {}
|
||||||
@@ -79,7 +85,7 @@ function use_core(path) {
|
|||||||
var script_blob = fd.slurp(file_path)
|
var script_blob = fd.slurp(file_path)
|
||||||
var script = text(script_blob)
|
var script = text(script_blob)
|
||||||
var mod = `(function setup_module(use){${script}})`
|
var mod = `(function setup_module(use){${script}})`
|
||||||
var fn = js.eval('core:' + path, mod)
|
var fn = mach_eval('core:' + path, mod)
|
||||||
var result = call(fn,sym, [use_core])
|
var result = call(fn,sym, [use_core])
|
||||||
use_cache[cache_key] = result;
|
use_cache[cache_key] = result;
|
||||||
return result;
|
return result;
|
||||||
@@ -133,32 +139,27 @@ function log(name, args) {
|
|||||||
var caller = caller_data(1)
|
var caller = caller_data(1)
|
||||||
var msg = args[0]
|
var msg = args[0]
|
||||||
|
|
||||||
switch(name) {
|
if (name == 'console') {
|
||||||
case 'console':
|
os.print(console_rec(caller.line, caller.file, msg))
|
||||||
os.print(console_rec(caller.line, caller.file, msg))
|
} else if (name == 'error') {
|
||||||
break
|
if (msg == null) msg = Error()
|
||||||
case 'error':
|
if (is_proto(msg, Error))
|
||||||
msg = msg ?? Error()
|
msg = msg.name + ": " + msg.message + "\n" + msg.stack
|
||||||
if (is_proto(msg, Error))
|
os.print(console_rec(caller.line, caller.file, msg))
|
||||||
msg = msg.name + ": " + msg.message + "\n" + msg.stack
|
} else if (name == 'system') {
|
||||||
os.print(console_rec(caller.line, caller.file, msg))
|
msg = "[SYSTEM] " + msg
|
||||||
break
|
os.print(console_rec(caller.line, caller.file, msg))
|
||||||
case 'system':
|
} else {
|
||||||
msg = "[SYSTEM] " + msg
|
log.console(`unknown log type: ${name}`)
|
||||||
os.print(console_rec(caller.line, caller.file, msg))
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
log.console(`unknown log type: ${name}`)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function disrupt(err)
|
function actor_die(err)
|
||||||
{
|
{
|
||||||
if (is_function(err.toString)) {
|
if (err && is_function(err.toString)) {
|
||||||
os.print(err.toString())
|
os.print(err.toString())
|
||||||
os.print("\n")
|
os.print("\n")
|
||||||
os.print(err.stack)
|
if (err.stack) os.print(err.stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overling) {
|
if (overling) {
|
||||||
@@ -185,14 +186,14 @@ function disrupt(err)
|
|||||||
log.console(err.stack)
|
log.console(err.stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
actor_mod.disrupt()
|
actor_mod["disrupt"]()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
actor_mod.on_exception(disrupt)
|
actor_mod.on_exception(actor_die)
|
||||||
|
|
||||||
_cell.args = init ?? {}
|
_cell.args = init != null ? init : {}
|
||||||
_cell.id = "newguy"
|
_cell.id = "newguy"
|
||||||
|
|
||||||
function create_actor(desc = {id:guid()}) {
|
function create_actor(desc = {id:guid()}) {
|
||||||
@@ -241,11 +242,15 @@ os.runtime_env = runtime_env
|
|||||||
|
|
||||||
$_.time_limit = function(requestor, seconds)
|
$_.time_limit = function(requestor, seconds)
|
||||||
{
|
{
|
||||||
if (!pronto.is_requestor(requestor))
|
if (!pronto.is_requestor(requestor)) {
|
||||||
throw Error('time_limit: first argument must be a requestor');
|
log.error('time_limit: first argument must be a requestor')
|
||||||
if (!is_number(seconds) || seconds <= 0)
|
disrupt
|
||||||
throw Error('time_limit: seconds must be a positive number');
|
}
|
||||||
|
if (!is_number(seconds) || seconds <= 0) {
|
||||||
|
log.error('time_limit: seconds must be a positive number')
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
return function time_limit_requestor(callback, value) {
|
return function time_limit_requestor(callback, value) {
|
||||||
pronto.check_callback(callback, 'time_limit')
|
pronto.check_callback(callback, 'time_limit')
|
||||||
var finished = false
|
var finished = false
|
||||||
@@ -260,7 +265,14 @@ $_.time_limit = function(requestor, seconds)
|
|||||||
timer_cancel = null
|
timer_cancel = null
|
||||||
}
|
}
|
||||||
if (requestor_cancel) {
|
if (requestor_cancel) {
|
||||||
try { pronto.requestor_cancel(reason) } catch (_) {}
|
requestor_cancel(reason)
|
||||||
|
requestor_cancel = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function safe_cancel_requestor(reason) {
|
||||||
|
if (requestor_cancel) {
|
||||||
|
requestor_cancel(reason)
|
||||||
requestor_cancel = null
|
requestor_cancel = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -268,15 +280,12 @@ $_.time_limit = function(requestor, seconds)
|
|||||||
timer_cancel = $_.delay(function() {
|
timer_cancel = $_.delay(function() {
|
||||||
if (finished) return
|
if (finished) return
|
||||||
def reason = make_reason(factory, 'Timeout.', seconds)
|
def reason = make_reason(factory, 'Timeout.', seconds)
|
||||||
if (requestor_cancel) {
|
safe_cancel_requestor(reason)
|
||||||
try { requestor_cancel(reason) } catch (_) {}
|
|
||||||
requestor_cancel = null
|
|
||||||
}
|
|
||||||
finished = true
|
finished = true
|
||||||
callback(null, reason)
|
callback(null, reason)
|
||||||
}, seconds)
|
}, seconds)
|
||||||
|
|
||||||
try {
|
function do_request() {
|
||||||
requestor_cancel = requestor(function(val, reason) {
|
requestor_cancel = requestor(function(val, reason) {
|
||||||
if (finished) return
|
if (finished) return
|
||||||
finished = true
|
finished = true
|
||||||
@@ -286,16 +295,14 @@ $_.time_limit = function(requestor, seconds)
|
|||||||
}
|
}
|
||||||
callback(val, reason)
|
callback(val, reason)
|
||||||
}, value)
|
}, value)
|
||||||
} catch (ex) {
|
} disruption {
|
||||||
cancel(ex)
|
cancel(Error('requestor failed'))
|
||||||
callback(null, ex)
|
callback(null, Error('requestor failed'))
|
||||||
}
|
}
|
||||||
|
do_request()
|
||||||
|
|
||||||
return function(reason) {
|
return function(reason) {
|
||||||
if (requestor_cancel) {
|
safe_cancel_requestor(reason)
|
||||||
try { requestor_cancel(reason) } catch (_) {}
|
|
||||||
requestor_cancel = null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -408,52 +415,54 @@ var portal_fn = null
|
|||||||
|
|
||||||
// takes a function input value that will eventually be called with the current time in number form.
|
// takes a function input value that will eventually be called with the current time in number form.
|
||||||
$_.portal = function(fn, port) {
|
$_.portal = function(fn, port) {
|
||||||
if (portal) throw Error(`Already started a portal listening on ${portal.port}`)
|
if (portal) {
|
||||||
if (!port) throw Error("Requires a valid port.")
|
log.error(`Already started a portal listening on ${portal.port}`)
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
if (!port) {
|
||||||
|
log.error("Requires a valid port.")
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
log.system(`starting a portal on port ${port}`)
|
log.system(`starting a portal on port ${port}`)
|
||||||
portal = enet.create_host({address: "any", port})
|
portal = enet.create_host({address: "any", port})
|
||||||
portal_fn = fn
|
portal_fn = fn
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_host(e) {
|
function handle_host(e) {
|
||||||
switch (e.type) {
|
if (e.type == "connect") {
|
||||||
case "connect":
|
log.system(`connected a new peer: ${e.peer.address}:${e.peer.port}`)
|
||||||
log.system(`connected a new peer: ${e.peer.address}:${e.peer.port}`)
|
peers[`${e.peer.address}:${e.peer.port}`] = e.peer
|
||||||
peers[`${e.peer.address}:${e.peer.port}`] = e.peer
|
var queue = peer_queue.get(e.peer)
|
||||||
var queue = peer_queue.get(e.peer)
|
if (queue) {
|
||||||
if (queue) {
|
arrfor(queue, (msg, index) => e.peer.send(nota.encode(msg)))
|
||||||
arrfor(queue, (msg, index) => e.peer.send(nota.encode(msg)))
|
log.system(`sent ${msg} out of queue`)
|
||||||
log.system(`sent ${msg} out of queue`)
|
|
||||||
peer_queue.delete(e.peer)
|
|
||||||
}
|
|
||||||
break
|
|
||||||
case "disconnect":
|
|
||||||
peer_queue.delete(e.peer)
|
peer_queue.delete(e.peer)
|
||||||
arrfor(array(peers), function(id, index) {
|
}
|
||||||
if (peers[id] == e.peer) delete peers[id]
|
} else if (e.type == "disconnect") {
|
||||||
|
peer_queue.delete(e.peer)
|
||||||
|
arrfor(array(peers), function(id, index) {
|
||||||
|
if (peers[id] == e.peer) delete peers[id]
|
||||||
|
})
|
||||||
|
log.system('portal got disconnect from ' + e.peer.address + ":" + e.peer.port)
|
||||||
|
} else if (e.type == "receive") {
|
||||||
|
var data = nota.decode(e.data)
|
||||||
|
if (data.replycc && !data.replycc.address) {
|
||||||
|
data.replycc[ACTORDATA].address = e.peer.address
|
||||||
|
data.replycc[ACTORDATA].port = e.peer.port
|
||||||
|
}
|
||||||
|
function populate_actor_addresses(obj) {
|
||||||
|
if (!is_object(obj)) return
|
||||||
|
if (obj[ACTORDATA] && !obj[ACTORDATA].address) {
|
||||||
|
obj[ACTORDATA].address = e.peer.address
|
||||||
|
obj[ACTORDATA].port = e.peer.port
|
||||||
|
}
|
||||||
|
arrfor(array(obj), function(key, index) {
|
||||||
|
if (key in obj)
|
||||||
|
populate_actor_addresses(obj[key])
|
||||||
})
|
})
|
||||||
log.system('portal got disconnect from ' + e.peer.address + ":" + e.peer.port)
|
}
|
||||||
break
|
if (data.data) populate_actor_addresses(data.data)
|
||||||
case "receive":
|
turn(data)
|
||||||
var data = nota.decode(e.data)
|
|
||||||
if (data.replycc && !data.replycc.address) {
|
|
||||||
data.replycc[ACTORDATA].address = e.peer.address
|
|
||||||
data.replycc[ACTORDATA].port = e.peer.port
|
|
||||||
}
|
|
||||||
function populate_actor_addresses(obj) {
|
|
||||||
if (!is_object(obj)) return
|
|
||||||
if (obj[ACTORDATA] && !obj[ACTORDATA].address) {
|
|
||||||
obj[ACTORDATA].address = e.peer.address
|
|
||||||
obj[ACTORDATA].port = e.peer.port
|
|
||||||
}
|
|
||||||
arrfor(array(obj), function(key, index) {
|
|
||||||
if (key in obj)
|
|
||||||
populate_actor_addresses(obj[key])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (data.data) populate_actor_addresses(data.data)
|
|
||||||
turn(data)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -487,10 +496,14 @@ $_.stop = function stop(actor) {
|
|||||||
need_stop = true
|
need_stop = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!is_actor(actor))
|
if (!is_actor(actor)) {
|
||||||
throw Error('Can only call stop on an actor.')
|
log.error('Can only call stop on an actor.')
|
||||||
if (is_null(underlings[actor[ACTORDATA].id]))
|
disrupt
|
||||||
throw Error('Can only call stop on an underling or self.')
|
}
|
||||||
|
if (is_null(underlings[actor[ACTORDATA].id])) {
|
||||||
|
log.error('Can only call stop on an underling or self.')
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
sys_msg(actor, {kind:"stop"})
|
sys_msg(actor, {kind:"stop"})
|
||||||
}
|
}
|
||||||
@@ -527,20 +540,22 @@ function actor_prep(actor, send) {
|
|||||||
|
|
||||||
// Send a message immediately without queuing
|
// Send a message immediately without queuing
|
||||||
function actor_send_immediate(actor, send) {
|
function actor_send_immediate(actor, send) {
|
||||||
try {
|
actor_send(actor, send)
|
||||||
actor_send(actor, send);
|
|
||||||
} catch (err) {
|
|
||||||
log.error("Failed to send immediate message:", err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function actor_send(actor, message) {
|
function actor_send(actor, message) {
|
||||||
if (actor[HEADER] && !actor[HEADER].replycc) // attempting to respond to a message but sender is not expecting; silently drop
|
if (actor[HEADER] && !actor[HEADER].replycc) // attempting to respond to a message but sender is not expecting; silently drop
|
||||||
return
|
return
|
||||||
|
|
||||||
if (!is_actor(actor) && !is_actor(actor.replycc)) throw Error(`Must send to an actor object. Attempted send to ${actor}`)
|
if (!is_actor(actor) && !is_actor(actor.replycc)) {
|
||||||
|
log.error(`Must send to an actor object. Attempted send to ${actor}`)
|
||||||
if (!is_object(message)) throw Error('Must send an object record.')
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_object(message)) {
|
||||||
|
log.error('Must send an object record.')
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
// message to self
|
// message to self
|
||||||
if (actor[ACTORDATA].id == _cell.id) {
|
if (actor[ACTORDATA].id == _cell.id) {
|
||||||
@@ -583,12 +598,10 @@ function actor_send(actor, message) {
|
|||||||
// Holds all messages queued during the current turn.
|
// Holds all messages queued during the current turn.
|
||||||
var message_queue = []
|
var message_queue = []
|
||||||
|
|
||||||
var need_stop = false
|
function send_messages() {
|
||||||
|
|
||||||
function send_messages() {
|
|
||||||
// if we've been flagged to stop, bail out before doing anything
|
// if we've been flagged to stop, bail out before doing anything
|
||||||
if (need_stop) {
|
if (need_stop) {
|
||||||
disrupt()
|
actor_die()
|
||||||
message_queue = []
|
message_queue = []
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -608,19 +621,26 @@ var need_stop = false
|
|||||||
var replies = {}
|
var replies = {}
|
||||||
|
|
||||||
function send(actor, message, reply) {
|
function send(actor, message, reply) {
|
||||||
if (!is_object(actor))
|
if (!is_object(actor)) {
|
||||||
throw Error(`Must send to an actor object. Provided: ${actor}`);
|
log.error(`Must send to an actor object. Provided: ${actor}`)
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_object(message))
|
if (!is_object(message)) {
|
||||||
throw Error('Message must be an object')
|
log.error('Message must be an object')
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
var send_msg = {type:"user", data: message}
|
var send_msg = {type:"user", data: message}
|
||||||
|
var target = actor
|
||||||
|
|
||||||
if (actor[HEADER] && actor[HEADER].replycc) {
|
if (actor[HEADER] && actor[HEADER].replycc) {
|
||||||
var header = actor[HEADER]
|
var header = actor[HEADER]
|
||||||
if (!header.replycc || !is_actor(header.replycc))
|
if (!header.replycc || !is_actor(header.replycc)) {
|
||||||
throw Error(`Supplied actor had a return, but it's not a valid actor! ${actor[HEADER]}`)
|
log.error(`Supplied actor had a return, but it's not a valid actor! ${actor[HEADER]}`)
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
actor = header.replycc
|
target = header.replycc
|
||||||
send_msg.return = header.reply
|
send_msg.return = header.reply
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -638,7 +658,7 @@ function send(actor, message, reply) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Instead of sending immediately, queue it
|
// Instead of sending immediately, queue it
|
||||||
actor_prep(actor, send_msg);
|
actor_prep(target, send_msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
stone(send)
|
stone(send)
|
||||||
@@ -669,7 +689,7 @@ overling = _cell.args.overling
|
|||||||
$_.overling = overling
|
$_.overling = overling
|
||||||
|
|
||||||
root = _cell.args.root
|
root = _cell.args.root
|
||||||
root ??= $_.self
|
if (root == null) root = $_.self
|
||||||
|
|
||||||
if (overling) {
|
if (overling) {
|
||||||
$_.couple(overling) // auto couple to overling
|
$_.couple(overling) // auto couple to overling
|
||||||
@@ -705,36 +725,35 @@ function handle_actor_disconnect(id) {
|
|||||||
delete greeters[id]
|
delete greeters[id]
|
||||||
}
|
}
|
||||||
log.system(`actor ${id} disconnected`)
|
log.system(`actor ${id} disconnected`)
|
||||||
if (!is_null(couplings[id])) disrupt("coupled actor died") // couplings now disrupts instead of stop
|
if (!is_null(couplings[id])) actor_die("coupled actor died") // couplings now disrupts instead of stop
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_sysym(msg)
|
function handle_sysym(msg)
|
||||||
{
|
{
|
||||||
var from
|
var from
|
||||||
switch(msg.kind) {
|
if (msg.kind == 'stop') {
|
||||||
case 'stop':
|
actor_die("got stop message")
|
||||||
disrupt("got stop message")
|
} else if (msg.kind == 'underling') {
|
||||||
break
|
from = msg.from
|
||||||
case 'underling':
|
var greeter = greeters[from[ACTORDATA].id]
|
||||||
from = msg.from
|
if (greeter) greeter(msg.message)
|
||||||
var greeter = greeters[from[ACTORDATA].id]
|
if (msg.message.type == 'disrupt')
|
||||||
if (greeter) greeter(msg.message)
|
delete underlings[from[ACTORDATA].id]
|
||||||
if (msg.message.type == 'disrupt')
|
} else if (msg.kind == 'contact') {
|
||||||
delete underlings[from[ACTORDATA].id]
|
if (portal_fn) {
|
||||||
break
|
var letter2 = msg.data
|
||||||
case 'contact':
|
letter2[HEADER] = msg
|
||||||
if (portal_fn) {
|
delete msg.data
|
||||||
var letter2 = msg.data
|
portal_fn(letter2)
|
||||||
letter2[HEADER] = msg
|
} else {
|
||||||
delete msg.data
|
log.error('Got a contact message, but no portal is established.')
|
||||||
portal_fn(letter2)
|
disrupt
|
||||||
} else throw Error('Got a contact message, but no portal is established.')
|
}
|
||||||
break
|
} else if (msg.kind == 'couple') {
|
||||||
case 'couple': // from must be notified when we die
|
// from must be notified when we die
|
||||||
from = msg.from
|
from = msg.from
|
||||||
underlings[from[ACTORDATA].id] = true
|
underlings[from[ACTORDATA].id] = true
|
||||||
log.system(`actor ${from} is coupled to me`)
|
log.system(`actor ${from} is coupled to me`)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -744,30 +763,27 @@ function handle_message(msg) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (msg.type) {
|
if (msg.type == "user") {
|
||||||
case "user":
|
var letter = msg.data // what the sender really sent
|
||||||
var letter = msg.data // what the sender really sent
|
_ObjectDefineProperty(letter, HEADER, {
|
||||||
_ObjectDefineProperty(letter, HEADER, {
|
value: msg, enumerable: false
|
||||||
value: msg, enumerable: false
|
})
|
||||||
})
|
_ObjectDefineProperty(letter, ACTORDATA, { // this is so is_actor == true
|
||||||
_ObjectDefineProperty(letter, ACTORDATA, { // this is so is_actor == true
|
value: { reply: msg.reply }, enumerable: false
|
||||||
value: { reply: msg.reply }, enumerable: false
|
})
|
||||||
})
|
|
||||||
|
if (msg.return) {
|
||||||
if (msg.return) {
|
var fn = replies[msg.return]
|
||||||
var fn = replies[msg.return]
|
if (fn) fn(letter)
|
||||||
if (fn) fn(letter)
|
delete replies[msg.return]
|
||||||
delete replies[msg.return]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (receive_fn) receive_fn(letter)
|
|
||||||
return
|
return
|
||||||
case "stopped":
|
}
|
||||||
handle_actor_disconnect(msg.id)
|
|
||||||
break
|
if (receive_fn) receive_fn(letter)
|
||||||
|
} else if (msg.type == "stopped") {
|
||||||
|
handle_actor_disconnect(msg.id)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
function enet_check()
|
function enet_check()
|
||||||
{
|
{
|
||||||
@@ -792,8 +808,10 @@ if (!locator) {
|
|||||||
locator = shop.resolve_locator(_cell.args.program + ".ce", pkg)
|
locator = shop.resolve_locator(_cell.args.program + ".ce", pkg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!locator)
|
if (!locator) {
|
||||||
throw Error(`Main program ${_cell.args.program} could not be found`)
|
os.print(`Main program ${_cell.args.program} could not be found\n`)
|
||||||
|
os.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
$_.clock(_ => {
|
$_.clock(_ => {
|
||||||
// Get capabilities for the main program
|
// Get capabilities for the main program
|
||||||
@@ -818,7 +836,6 @@ $_.clock(_ => {
|
|||||||
var val = call(locator.symbol, null, [_cell.args.arg, use_fn, env])
|
var val = call(locator.symbol, null, [_cell.args.arg, use_fn, env])
|
||||||
|
|
||||||
if (val)
|
if (val)
|
||||||
throw Error('Program must not return anything');
|
log.error('Program must not return anything')
|
||||||
|
disrupt
|
||||||
})
|
})
|
||||||
|
|
||||||
})()
|
|
||||||
@@ -577,7 +577,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
|||||||
|
|
||||||
JSValue js_os_use(JSContext *js) {
|
JSValue js_os_use(JSContext *js) {
|
||||||
JS_NewClassID(&js_dylib_class_id);
|
JS_NewClassID(&js_dylib_class_id);
|
||||||
JS_NewClass(JS_GetRuntime(js), js_dylib_class_id, &js_dylib_class);
|
JS_NewClass(js, js_dylib_class_id, &js_dylib_class);
|
||||||
|
|
||||||
JSValue mod = JS_NewObject(js);
|
JSValue mod = JS_NewObject(js);
|
||||||
JS_SetPropertyFunctionList(js,mod,js_os_funcs,countof(js_os_funcs));
|
JS_SetPropertyFunctionList(js,mod,js_os_funcs,countof(js_os_funcs));
|
||||||
|
|||||||
@@ -44,7 +44,12 @@ src += [ # core
|
|||||||
'wildmatch.c',
|
'wildmatch.c',
|
||||||
'qjs_actor.c',
|
'qjs_actor.c',
|
||||||
'miniz.c',
|
'miniz.c',
|
||||||
'quickjs.c',
|
'runtime.c',
|
||||||
|
'cell_js.c',
|
||||||
|
'tokenize.c',
|
||||||
|
'parse.c',
|
||||||
|
'mach.c',
|
||||||
|
'mcode.c',
|
||||||
'libregexp.c', 'libunicode.c', 'cutils.c', 'dtoa.c'
|
'libregexp.c', 'libunicode.c', 'cutils.c', 'dtoa.c'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -571,13 +571,13 @@ static const JSCFunctionListEntry js_enet_peer_funcs[] = {
|
|||||||
JSValue js_enet_use(JSContext *ctx)
|
JSValue js_enet_use(JSContext *ctx)
|
||||||
{
|
{
|
||||||
JS_NewClassID(&enet_host_id);
|
JS_NewClassID(&enet_host_id);
|
||||||
JS_NewClass(JS_GetRuntime(ctx), enet_host_id, &enet_host);
|
JS_NewClass(ctx, enet_host_id, &enet_host);
|
||||||
JSValue host_proto = JS_NewObject(ctx);
|
JSValue host_proto = JS_NewObject(ctx);
|
||||||
JS_SetPropertyFunctionList(ctx, host_proto, js_enet_host_funcs, countof(js_enet_host_funcs));
|
JS_SetPropertyFunctionList(ctx, host_proto, js_enet_host_funcs, countof(js_enet_host_funcs));
|
||||||
JS_SetClassProto(ctx, enet_host_id, host_proto);
|
JS_SetClassProto(ctx, enet_host_id, host_proto);
|
||||||
|
|
||||||
JS_NewClassID(&enet_peer_class_id);
|
JS_NewClassID(&enet_peer_class_id);
|
||||||
JS_NewClass(JS_GetRuntime(ctx), enet_peer_class_id, &enet_peer_class);
|
JS_NewClass(ctx, enet_peer_class_id, &enet_peer_class);
|
||||||
JSValue peer_proto = JS_NewObject(ctx);
|
JSValue peer_proto = JS_NewObject(ctx);
|
||||||
JS_SetPropertyFunctionList(ctx, peer_proto, js_enet_peer_funcs, countof(js_enet_peer_funcs));
|
JS_SetPropertyFunctionList(ctx, peer_proto, js_enet_peer_funcs, countof(js_enet_peer_funcs));
|
||||||
JS_SetClassProto(ctx, enet_peer_class_id, peer_proto);
|
JS_SetClassProto(ctx, enet_peer_class_id, peer_proto);
|
||||||
|
|||||||
4
qop.c
4
qop.c
@@ -458,13 +458,13 @@ static const JSCFunctionListEntry js_qop_funcs[] = {
|
|||||||
|
|
||||||
JSValue js_qop_use(JSContext *js) {
|
JSValue js_qop_use(JSContext *js) {
|
||||||
JS_NewClassID(&js_qop_archive_class_id);
|
JS_NewClassID(&js_qop_archive_class_id);
|
||||||
JS_NewClass(JS_GetRuntime(js), js_qop_archive_class_id, &js_qop_archive_class);
|
JS_NewClass(js, js_qop_archive_class_id, &js_qop_archive_class);
|
||||||
JSValue archive_proto = JS_NewObject(js);
|
JSValue archive_proto = JS_NewObject(js);
|
||||||
JS_SetPropertyFunctionList(js, archive_proto, js_qop_archive_funcs, countof(js_qop_archive_funcs));
|
JS_SetPropertyFunctionList(js, archive_proto, js_qop_archive_funcs, countof(js_qop_archive_funcs));
|
||||||
JS_SetClassProto(js, js_qop_archive_class_id, archive_proto);
|
JS_SetClassProto(js, js_qop_archive_class_id, archive_proto);
|
||||||
|
|
||||||
JS_NewClassID(&js_qop_writer_class_id);
|
JS_NewClassID(&js_qop_writer_class_id);
|
||||||
JS_NewClass(JS_GetRuntime(js), js_qop_writer_class_id, &js_qop_writer_class);
|
JS_NewClass(js, js_qop_writer_class_id, &js_qop_writer_class);
|
||||||
JSValue writer_proto = JS_NewObject(js);
|
JSValue writer_proto = JS_NewObject(js);
|
||||||
JS_SetPropertyFunctionList(js, writer_proto, js_qop_writer_funcs, countof(js_qop_writer_funcs));
|
JS_SetPropertyFunctionList(js, writer_proto, js_qop_writer_funcs, countof(js_qop_writer_funcs));
|
||||||
JS_SetClassProto(js, js_qop_writer_class_id, writer_proto);
|
JS_SetClassProto(js, js_qop_writer_class_id, writer_proto);
|
||||||
|
|||||||
165
source/cell.c
165
source/cell.c
@@ -26,6 +26,7 @@ static int run_test_suite(size_t heap_size);
|
|||||||
|
|
||||||
cell_rt *root_cell = NULL;
|
cell_rt *root_cell = NULL;
|
||||||
static char *core_path = NULL;
|
static char *core_path = NULL;
|
||||||
|
static JSRuntime *g_runtime = NULL;
|
||||||
|
|
||||||
// Get the home directory
|
// Get the home directory
|
||||||
static const char* get_home_dir(void) {
|
static const char* get_home_dir(void) {
|
||||||
@@ -105,35 +106,46 @@ static char* load_core_file(const char *filename, size_t *out_size) {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int print_json_errors(const char *json) {
|
static int print_tree_errors(cJSON *root) {
|
||||||
if (!json) return 0;
|
|
||||||
cJSON *root = cJSON_Parse(json);
|
|
||||||
if (!root) return 0;
|
if (!root) return 0;
|
||||||
cJSON *errors = cJSON_GetObjectItemCaseSensitive(root, "errors");
|
cJSON *errors = cJSON_GetObjectItemCaseSensitive(root, "errors");
|
||||||
if (!cJSON_IsArray(errors) || cJSON_GetArraySize(errors) == 0) {
|
if (!cJSON_IsArray(errors) || cJSON_GetArraySize(errors) == 0)
|
||||||
cJSON_Delete(root);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
const char *filename = "<unknown>";
|
const char *filename = "<unknown>";
|
||||||
cJSON *fname = cJSON_GetObjectItemCaseSensitive(root, "filename");
|
cJSON *fname = cJSON_GetObjectItemCaseSensitive(root, "filename");
|
||||||
if (cJSON_IsString(fname))
|
if (cJSON_IsString(fname))
|
||||||
filename = fname->valuestring;
|
filename = fname->valuestring;
|
||||||
|
int prev_line = -1;
|
||||||
|
const char *prev_msg = NULL;
|
||||||
cJSON *e;
|
cJSON *e;
|
||||||
cJSON_ArrayForEach(e, errors) {
|
cJSON_ArrayForEach(e, errors) {
|
||||||
const char *msg = cJSON_GetStringValue(
|
const char *msg = cJSON_GetStringValue(
|
||||||
cJSON_GetObjectItemCaseSensitive(e, "message"));
|
cJSON_GetObjectItemCaseSensitive(e, "message"));
|
||||||
cJSON *line = cJSON_GetObjectItemCaseSensitive(e, "line");
|
cJSON *line = cJSON_GetObjectItemCaseSensitive(e, "line");
|
||||||
cJSON *col = cJSON_GetObjectItemCaseSensitive(e, "column");
|
cJSON *col = cJSON_GetObjectItemCaseSensitive(e, "column");
|
||||||
|
int cur_line = cJSON_IsNumber(line) ? (int)line->valuedouble : -1;
|
||||||
|
if (prev_msg && msg && cur_line == prev_line && strcmp(msg, prev_msg) == 0)
|
||||||
|
continue;
|
||||||
|
prev_line = cur_line;
|
||||||
|
prev_msg = msg;
|
||||||
if (msg && cJSON_IsNumber(line) && cJSON_IsNumber(col))
|
if (msg && cJSON_IsNumber(line) && cJSON_IsNumber(col))
|
||||||
fprintf(stderr, "%s:%d:%d: error: %s\n",
|
fprintf(stderr, "%s:%d:%d: error: %s\n",
|
||||||
filename, (int)line->valuedouble, (int)col->valuedouble, msg);
|
filename, (int)line->valuedouble, (int)col->valuedouble, msg);
|
||||||
else if (msg)
|
else if (msg)
|
||||||
fprintf(stderr, "%s: error: %s\n", filename, msg);
|
fprintf(stderr, "%s: error: %s\n", filename, msg);
|
||||||
}
|
}
|
||||||
cJSON_Delete(root);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int print_json_errors(const char *json) {
|
||||||
|
if (!json) return 0;
|
||||||
|
cJSON *root = cJSON_Parse(json);
|
||||||
|
if (!root) return 0;
|
||||||
|
int result = print_tree_errors(root);
|
||||||
|
cJSON_Delete(root);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// Get the core path for use by scripts
|
// Get the core path for use by scripts
|
||||||
const char* cell_get_core_path(void) {
|
const char* cell_get_core_path(void) {
|
||||||
return core_path;
|
return core_path;
|
||||||
@@ -154,17 +166,31 @@ JSValue js_wota_use(JSContext *js);
|
|||||||
|
|
||||||
void script_startup(cell_rt *prt)
|
void script_startup(cell_rt *prt)
|
||||||
{
|
{
|
||||||
JSRuntime *rt = JS_NewRuntime();
|
if (!g_runtime) {
|
||||||
JS_SetInterruptHandler(rt, (JSInterruptHandler *)actor_interrupt_cb, prt);
|
g_runtime = JS_NewRuntime();
|
||||||
JSContext *js = JS_NewContext(rt);
|
}
|
||||||
|
JSContext *js = JS_NewContext(g_runtime);
|
||||||
|
JS_SetInterruptHandler(js, (JSInterruptHandler *)actor_interrupt_cb, prt);
|
||||||
|
|
||||||
JS_SetContextOpaque(js, prt);
|
JS_SetContextOpaque(js, prt);
|
||||||
prt->context = js;
|
prt->context = js;
|
||||||
|
|
||||||
|
/* Register all GCRef fields so the Cheney GC can relocate them. */
|
||||||
|
JS_AddGCRef(js, &prt->idx_buffer_ref);
|
||||||
|
JS_AddGCRef(js, &prt->on_exception_ref);
|
||||||
|
JS_AddGCRef(js, &prt->message_handle_ref);
|
||||||
|
JS_AddGCRef(js, &prt->unneeded_ref);
|
||||||
|
JS_AddGCRef(js, &prt->actor_sym_ref);
|
||||||
|
prt->idx_buffer_ref.val = JS_NULL;
|
||||||
|
prt->on_exception_ref.val = JS_NULL;
|
||||||
|
prt->message_handle_ref.val = JS_NULL;
|
||||||
|
prt->unneeded_ref.val = JS_NULL;
|
||||||
|
prt->actor_sym_ref.val = JS_NULL;
|
||||||
|
|
||||||
cell_rt *crt = JS_GetContextOpaque(js);
|
cell_rt *crt = JS_GetContextOpaque(js);
|
||||||
JS_FreeValue(js, js_blob_use(js));
|
JS_FreeValue(js, js_blob_use(js));
|
||||||
|
|
||||||
// Load and compile engine.cm
|
// Load and parse engine.cm to AST
|
||||||
size_t engine_size;
|
size_t engine_size;
|
||||||
char *data = load_core_file(ENGINE, &engine_size);
|
char *data = load_core_file(ENGINE, &engine_size);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
@@ -172,10 +198,15 @@ void script_startup(cell_rt *prt)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSValue bytecode = JS_Compile(js, data, engine_size, ENGINE);
|
cJSON *ast = JS_ASTTree(data, engine_size, ENGINE);
|
||||||
free(data);
|
free(data);
|
||||||
if (JS_IsException(bytecode)) {
|
if (!ast) {
|
||||||
uncaught_exception(js, bytecode);
|
printf("ERROR: Failed to parse %s\n", ENGINE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (print_tree_errors(ast)) {
|
||||||
|
cJSON_Delete(ast);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,8 +217,8 @@ void script_startup(cell_rt *prt)
|
|||||||
JS_SetPropertyStr(js, hidden_env, "nota", js_nota_use(js));
|
JS_SetPropertyStr(js, hidden_env, "nota", js_nota_use(js));
|
||||||
JS_SetPropertyStr(js, hidden_env, "wota", js_wota_use(js));
|
JS_SetPropertyStr(js, hidden_env, "wota", js_wota_use(js));
|
||||||
|
|
||||||
crt->actor_sym = JS_NewObject(js);
|
crt->actor_sym_ref.val = JS_NewObject(js);
|
||||||
JS_SetPropertyStr(js, hidden_env, "actorsym", JS_DupValue(js, crt->actor_sym));
|
JS_SetPropertyStr(js, hidden_env, "actorsym", JS_DupValue(js, crt->actor_sym_ref.val));
|
||||||
|
|
||||||
// Always set init (even if null)
|
// Always set init (even if null)
|
||||||
if (crt->init_wota) {
|
if (crt->init_wota) {
|
||||||
@@ -205,9 +236,10 @@ void script_startup(cell_rt *prt)
|
|||||||
// Stone the environment
|
// Stone the environment
|
||||||
hidden_env = JS_Stone(js, hidden_env);
|
hidden_env = JS_Stone(js, hidden_env);
|
||||||
|
|
||||||
// Integrate and run
|
// Run through MACH VM
|
||||||
crt->state = ACTOR_RUNNING;
|
crt->state = ACTOR_RUNNING;
|
||||||
JSValue v = JS_Integrate(js, bytecode, hidden_env);
|
JSValue v = JS_RunMachTree(js, ast, hidden_env);
|
||||||
|
cJSON_Delete(ast);
|
||||||
uncaught_exception(js, v);
|
uncaught_exception(js, v);
|
||||||
crt->state = ACTOR_IDLE;
|
crt->state = ACTOR_IDLE;
|
||||||
set_actor_state(crt);
|
set_actor_state(crt);
|
||||||
@@ -399,11 +431,13 @@ int cell_init(int argc, char **argv)
|
|||||||
script = (char *)script_or_file;
|
script = (char *)script_or_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *json = JS_AST(script, strlen(script), filename);
|
cJSON *ast = JS_ASTTree(script, strlen(script), filename);
|
||||||
if (json) {
|
if (ast) {
|
||||||
int has_errors = print_json_errors(json);
|
int has_errors = print_tree_errors(ast);
|
||||||
printf("%s\n", json);
|
char *pretty = cJSON_Print(ast);
|
||||||
free(json);
|
cJSON_Delete(ast);
|
||||||
|
printf("%s\n", pretty);
|
||||||
|
free(pretty);
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return has_errors ? 1 : 0;
|
return has_errors ? 1 : 0;
|
||||||
} else {
|
} else {
|
||||||
@@ -445,8 +479,14 @@ int cell_init(int argc, char **argv)
|
|||||||
char *json = JS_Tokenize(script, strlen(script), filename);
|
char *json = JS_Tokenize(script, strlen(script), filename);
|
||||||
if (json) {
|
if (json) {
|
||||||
int has_errors = print_json_errors(json);
|
int has_errors = print_json_errors(json);
|
||||||
printf("%s\n", json);
|
cJSON *root = cJSON_Parse(json);
|
||||||
free(json);
|
free(json);
|
||||||
|
if (root) {
|
||||||
|
char *pretty = cJSON_Print(root);
|
||||||
|
cJSON_Delete(root);
|
||||||
|
printf("%s\n", pretty);
|
||||||
|
free(pretty);
|
||||||
|
}
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return has_errors ? 1 : 0;
|
return has_errors ? 1 : 0;
|
||||||
} else {
|
} else {
|
||||||
@@ -485,30 +525,32 @@ int cell_init(int argc, char **argv)
|
|||||||
script = (char *)script_or_file;
|
script = (char *)script_or_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
cJSON *ast = JS_ASTTree(script, strlen(script), filename);
|
||||||
if (!ast_json) {
|
if (!ast) {
|
||||||
printf("Failed to parse AST\n");
|
printf("Failed to parse AST\n");
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (print_json_errors(ast_json)) {
|
if (print_tree_errors(ast)) {
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *mcode_json = JS_Mcode(ast_json);
|
cJSON *mcode = JS_McodeTree(ast);
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
|
|
||||||
if (!mcode_json) {
|
if (!mcode) {
|
||||||
printf("Failed to generate MCODE\n");
|
printf("Failed to generate MCODE\n");
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", mcode_json);
|
char *pretty = cJSON_Print(mcode);
|
||||||
free(mcode_json);
|
cJSON_Delete(mcode);
|
||||||
|
printf("%s\n", pretty);
|
||||||
|
free(pretty);
|
||||||
|
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -536,40 +578,39 @@ int cell_init(int argc, char **argv)
|
|||||||
script = (char *)script_or_file;
|
script = (char *)script_or_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
cJSON *ast = JS_ASTTree(script, strlen(script), filename);
|
||||||
if (!ast_json) {
|
if (!ast) {
|
||||||
printf("Failed to parse AST\n");
|
printf("Failed to parse AST\n");
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (print_json_errors(ast_json)) {
|
if (print_tree_errors(ast)) {
|
||||||
free(ast_json); free(allocated_script);
|
cJSON_Delete(ast); free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *mcode_json = JS_Mcode(ast_json);
|
cJSON *mcode = JS_McodeTree(ast);
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
|
|
||||||
if (!mcode_json) {
|
if (!mcode) {
|
||||||
printf("Failed to generate MCODE\n");
|
printf("Failed to generate MCODE\n");
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (print_json_errors(mcode_json)) {
|
if (print_tree_errors(mcode)) {
|
||||||
free(mcode_json); free(allocated_script);
|
cJSON_Delete(mcode); free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Use a larger heap context for execution */
|
/* Use a larger heap context for execution */
|
||||||
JSRuntime *rt = JS_NewRuntime();
|
JSRuntime *rt = JS_NewRuntime();
|
||||||
if (!rt) { printf("Failed to create JS runtime\n"); free(mcode_json); free(allocated_script); return 1; }
|
if (!rt) { printf("Failed to create JS runtime\n"); cJSON_Delete(mcode); free(allocated_script); return 1; }
|
||||||
JSContext *ctx = JS_NewContextWithHeapSize(rt, 64 * 1024);
|
JSContext *ctx = JS_NewContextWithHeapSize(rt, 64 * 1024);
|
||||||
if (!ctx) { printf("Failed to create execution context\n"); free(mcode_json); JS_FreeRuntime(rt); free(allocated_script); return 1; }
|
if (!ctx) { printf("Failed to create execution context\n"); cJSON_Delete(mcode); JS_FreeRuntime(rt); free(allocated_script); return 1; }
|
||||||
|
|
||||||
JSValue result = JS_CallMcode(ctx, mcode_json);
|
JSValue result = JS_CallMcodeTree(ctx, mcode); /* takes ownership of mcode */
|
||||||
free(mcode_json);
|
|
||||||
|
|
||||||
if (JS_IsException(result)) {
|
if (JS_IsException(result)) {
|
||||||
JSValue exc = JS_GetException(ctx);
|
JSValue exc = JS_GetException(ctx);
|
||||||
@@ -629,15 +670,15 @@ int cell_init(int argc, char **argv)
|
|||||||
script = (char *)script_or_file;
|
script = (char *)script_or_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
cJSON *ast = JS_ASTTree(script, strlen(script), filename);
|
||||||
if (!ast_json) {
|
if (!ast) {
|
||||||
printf("Failed to parse AST\n");
|
printf("Failed to parse AST\n");
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (print_json_errors(ast_json)) {
|
if (print_tree_errors(ast)) {
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -645,18 +686,18 @@ int cell_init(int argc, char **argv)
|
|||||||
JSRuntime *rt = JS_NewRuntime();
|
JSRuntime *rt = JS_NewRuntime();
|
||||||
if (!rt) {
|
if (!rt) {
|
||||||
printf("Failed to create JS runtime\n");
|
printf("Failed to create JS runtime\n");
|
||||||
free(ast_json); free(allocated_script);
|
cJSON_Delete(ast); free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
JSContext *ctx = JS_NewContext(rt);
|
JSContext *ctx = JS_NewContext(rt);
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
printf("Failed to create JS context\n");
|
printf("Failed to create JS context\n");
|
||||||
free(ast_json); JS_FreeRuntime(rt); free(allocated_script);
|
cJSON_Delete(ast); JS_FreeRuntime(rt); free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_DumpMach(ctx, ast_json, JS_NULL);
|
JS_DumpMachTree(ctx, ast, JS_NULL);
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
|
|
||||||
JS_FreeContext(ctx);
|
JS_FreeContext(ctx);
|
||||||
JS_FreeRuntime(rt);
|
JS_FreeRuntime(rt);
|
||||||
@@ -693,15 +734,15 @@ int cell_init(int argc, char **argv)
|
|||||||
script = (char *)script_or_file;
|
script = (char *)script_or_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ast_json = JS_AST(script, strlen(script), filename);
|
cJSON *ast = JS_ASTTree(script, strlen(script), filename);
|
||||||
if (!ast_json) {
|
if (!ast) {
|
||||||
printf("Failed to parse AST\n");
|
printf("Failed to parse AST\n");
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (print_json_errors(ast_json)) {
|
if (print_tree_errors(ast)) {
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -709,18 +750,18 @@ int cell_init(int argc, char **argv)
|
|||||||
JSRuntime *rt = JS_NewRuntime();
|
JSRuntime *rt = JS_NewRuntime();
|
||||||
if (!rt) {
|
if (!rt) {
|
||||||
printf("Failed to create JS runtime\n");
|
printf("Failed to create JS runtime\n");
|
||||||
free(ast_json); free(allocated_script);
|
cJSON_Delete(ast); free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
JSContext *ctx = JS_NewContext(rt);
|
JSContext *ctx = JS_NewContext(rt);
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
printf("Failed to create JS context\n");
|
printf("Failed to create JS context\n");
|
||||||
free(ast_json); JS_FreeRuntime(rt); free(allocated_script);
|
cJSON_Delete(ast); JS_FreeRuntime(rt); free(allocated_script);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSValue result = JS_RunMach(ctx, ast_json, JS_NULL);
|
JSValue result = JS_RunMachTree(ctx, ast, JS_NULL);
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
|
|
||||||
int exit_code = 0;
|
int exit_code = 0;
|
||||||
if (JS_IsException(result)) {
|
if (JS_IsException(result)) {
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ JS_SetPropertyFunctionList(js, TYPE##_proto, js_##TYPE##_funcs, countof(js_##TYP
|
|||||||
|
|
||||||
#define QJSCLASSPREP_NO_FUNCS(TYPE) \
|
#define QJSCLASSPREP_NO_FUNCS(TYPE) \
|
||||||
JS_NewClassID(&js_##TYPE##_id);\
|
JS_NewClassID(&js_##TYPE##_id);\
|
||||||
JS_NewClass(JS_GetRuntime(js), js_##TYPE##_id, &js_##TYPE##_class);\
|
JS_NewClass(js, js_##TYPE##_id, &js_##TYPE##_class);\
|
||||||
JSValue TYPE##_proto = JS_NewObject(js); \
|
JSValue TYPE##_proto = JS_NewObject(js); \
|
||||||
JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
|
JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
|
||||||
|
|
||||||
|
|||||||
@@ -24,21 +24,26 @@ typedef struct letter {
|
|||||||
|
|
||||||
typedef struct cell_rt {
|
typedef struct cell_rt {
|
||||||
JSContext *context;
|
JSContext *context;
|
||||||
JSValue idx_buffer;
|
|
||||||
JSValue on_exception;
|
/* JSValues on the GC heap — each paired with a JSGCRef so the
|
||||||
JSValue message_handle;
|
Cheney GC can relocate them during compaction. */
|
||||||
|
JSGCRef idx_buffer_ref;
|
||||||
|
JSGCRef on_exception_ref;
|
||||||
|
JSGCRef message_handle_ref;
|
||||||
|
JSGCRef unneeded_ref;
|
||||||
|
JSGCRef actor_sym_ref;
|
||||||
|
|
||||||
void *init_wota;
|
void *init_wota;
|
||||||
|
|
||||||
/* Protects JSContext usage */
|
/* Protects JSContext usage */
|
||||||
pthread_mutex_t *mutex; /* for everything else */
|
pthread_mutex_t *mutex; /* for everything else */
|
||||||
pthread_mutex_t *msg_mutex; /* For message queue and timers queue */
|
pthread_mutex_t *msg_mutex; /* For message queue and timers queue */
|
||||||
|
|
||||||
char *id;
|
char *id;
|
||||||
|
|
||||||
int idx_count;
|
int idx_count;
|
||||||
|
|
||||||
/* The “mailbox” for incoming messages + a dedicated lock for it: */
|
/* The "mailbox" for incoming messages + a dedicated lock for it: */
|
||||||
letter *letters;
|
letter *letters;
|
||||||
|
|
||||||
/* CHANGED FOR EVENTS: a separate lock for the actor->events queue */
|
/* CHANGED FOR EVENTS: a separate lock for the actor->events queue */
|
||||||
@@ -47,14 +52,11 @@ typedef struct cell_rt {
|
|||||||
int state;
|
int state;
|
||||||
uint32_t ar; // timer for unneeded
|
uint32_t ar; // timer for unneeded
|
||||||
double ar_secs; // time for unneeded
|
double ar_secs; // time for unneeded
|
||||||
JSValue unneeded; // fn to call before unneeded
|
|
||||||
|
|
||||||
int disrupt;
|
int disrupt;
|
||||||
int main_thread_only;
|
int main_thread_only;
|
||||||
int affinity;
|
int affinity;
|
||||||
|
|
||||||
JSValue actor_sym;
|
|
||||||
|
|
||||||
const char *name; // human friendly name
|
const char *name; // human friendly name
|
||||||
cell_hook trace_hook;
|
cell_hook trace_hook;
|
||||||
} cell_rt;
|
} cell_rt;
|
||||||
@@ -63,8 +65,6 @@ cell_rt *create_actor(void *wota);
|
|||||||
const char *register_actor(const char *id, cell_rt *actor, int mainthread, double ar);
|
const char *register_actor(const char *id, cell_rt *actor, int mainthread, double ar);
|
||||||
void actor_disrupt(cell_rt *actor);
|
void actor_disrupt(cell_rt *actor);
|
||||||
|
|
||||||
JSValue actor_sym(cell_rt *actor);
|
|
||||||
|
|
||||||
const char *send_message(const char *id, void *msg);
|
const char *send_message(const char *id, void *msg);
|
||||||
const char *register_actor(const char *id, cell_rt *actor, int mainthread, double ar);
|
const char *register_actor(const char *id, cell_rt *actor, int mainthread, double ar);
|
||||||
void actor_unneeded(cell_rt *actor, JSValue fn, double seconds);
|
void actor_unneeded(cell_rt *actor, JSValue fn, double seconds);
|
||||||
|
|||||||
12740
source/cell_js.c
Normal file
12740
source/cell_js.c
Normal file
File diff suppressed because it is too large
Load Diff
3316
source/mach.c
Normal file
3316
source/mach.c
Normal file
File diff suppressed because it is too large
Load Diff
3461
source/mcode.c
Normal file
3461
source/mcode.c
Normal file
File diff suppressed because it is too large
Load Diff
2087
source/parse.c
Normal file
2087
source/parse.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -74,7 +74,7 @@ JSC_CCALL(os_register_actor,
|
|||||||
JS_ToFloat64(js, &ar, argv[3]);
|
JS_ToFloat64(js, &ar, argv[3]);
|
||||||
const char *err = register_actor(id, rt, JS_ToBool(js, argv[2]), ar);
|
const char *err = register_actor(id, rt, JS_ToBool(js, argv[2]), ar);
|
||||||
if (err) return JS_ThrowInternalError(js, "Could not register actor: %s", err);
|
if (err) return JS_ThrowInternalError(js, "Could not register actor: %s", err);
|
||||||
rt->message_handle = JS_DupValue(js, argv[1]);
|
rt->message_handle_ref.val = argv[1];
|
||||||
rt->context = js;
|
rt->context = js;
|
||||||
JS_FreeCString(js, id);
|
JS_FreeCString(js, id);
|
||||||
)
|
)
|
||||||
@@ -106,8 +106,7 @@ JSC_SCALL(actor_setname,
|
|||||||
|
|
||||||
JSC_CCALL(actor_on_exception,
|
JSC_CCALL(actor_on_exception,
|
||||||
cell_rt *rt = JS_GetContextOpaque(js);
|
cell_rt *rt = JS_GetContextOpaque(js);
|
||||||
JS_FreeValue(js, rt->on_exception);
|
rt->on_exception_ref.val = argv[0];
|
||||||
rt->on_exception = JS_DupValue(js,argv[0]);
|
|
||||||
)
|
)
|
||||||
|
|
||||||
JSC_CCALL(actor_clock,
|
JSC_CCALL(actor_clock,
|
||||||
|
|||||||
2007
source/quickjs-internal.h
Normal file
2007
source/quickjs-internal.h
Normal file
File diff suppressed because it is too large
Load Diff
35284
source/quickjs.c
35284
source/quickjs.c
File diff suppressed because it is too large
Load Diff
@@ -367,10 +367,10 @@ JSRuntime *JS_NewRuntime (void);
|
|||||||
void JS_SetRuntimeInfo (JSRuntime *rt, const char *info);
|
void JS_SetRuntimeInfo (JSRuntime *rt, const char *info);
|
||||||
void JS_SetMemoryLimit (JSRuntime *rt, size_t limit);
|
void JS_SetMemoryLimit (JSRuntime *rt, size_t limit);
|
||||||
/* use 0 to disable maximum stack size check */
|
/* use 0 to disable maximum stack size check */
|
||||||
void JS_SetMaxStackSize (JSRuntime *rt, size_t stack_size);
|
void JS_SetMaxStackSize (JSContext *ctx, size_t stack_size);
|
||||||
/* should be called when changing thread to update the stack top value
|
/* should be called when changing thread to update the stack top value
|
||||||
used to check stack overflow. */
|
used to check stack overflow. */
|
||||||
void JS_UpdateStackTop (JSRuntime *rt);
|
void JS_UpdateStackTop (JSContext *ctx);
|
||||||
void JS_FreeRuntime (JSRuntime *rt);
|
void JS_FreeRuntime (JSRuntime *rt);
|
||||||
void *JS_GetRuntimeOpaque (JSRuntime *rt);
|
void *JS_GetRuntimeOpaque (JSRuntime *rt);
|
||||||
void JS_SetRuntimeOpaque (JSRuntime *rt, void *opaque);
|
void JS_SetRuntimeOpaque (JSRuntime *rt, void *opaque);
|
||||||
@@ -429,9 +429,9 @@ JSClassID JS_NewClassID (JSClassID *pclass_id);
|
|||||||
/* Returns the class ID if `v` is an object, otherwise returns
|
/* Returns the class ID if `v` is an object, otherwise returns
|
||||||
* JS_INVALID_CLASS_ID. */
|
* JS_INVALID_CLASS_ID. */
|
||||||
JSClassID JS_GetClassID (JSValue v);
|
JSClassID JS_GetClassID (JSValue v);
|
||||||
int JS_NewClass (JSRuntime *rt, JSClassID class_id,
|
int JS_NewClass (JSContext *ctx, JSClassID class_id,
|
||||||
const JSClassDef *class_def);
|
const JSClassDef *class_def);
|
||||||
int JS_IsRegisteredClass (JSRuntime *rt, JSClassID class_id);
|
int JS_IsRegisteredClass (JSContext *ctx, JSClassID class_id);
|
||||||
|
|
||||||
extern JSClassID js_class_id_alloc;
|
extern JSClassID js_class_id_alloc;
|
||||||
|
|
||||||
@@ -740,7 +740,7 @@ JSValue JS_JSONStringify (JSContext *ctx, JSValue obj,
|
|||||||
|
|
||||||
/* return != 0 if the JS code needs to be interrupted */
|
/* return != 0 if the JS code needs to be interrupted */
|
||||||
typedef int JSInterruptHandler (JSRuntime *rt, void *opaque);
|
typedef int JSInterruptHandler (JSRuntime *rt, void *opaque);
|
||||||
void JS_SetInterruptHandler (JSRuntime *rt, JSInterruptHandler *cb,
|
void JS_SetInterruptHandler (JSContext *ctx, JSInterruptHandler *cb,
|
||||||
void *opaque);
|
void *opaque);
|
||||||
/* select which debug info is stripped from the compiled code */
|
/* select which debug info is stripped from the compiled code */
|
||||||
#define JS_STRIP_SOURCE (1 << 0) /* strip source code */
|
#define JS_STRIP_SOURCE (1 << 0) /* strip source code */
|
||||||
@@ -1218,45 +1218,57 @@ CellModule *cell_module_from_bytecode (JSContext *ctx, JSFunctionBytecode *main_
|
|||||||
Returns allocated CellModule (caller must free with cell_module_free), or NULL on error. */
|
Returns allocated CellModule (caller must free with cell_module_free), or NULL on error. */
|
||||||
CellModule *JS_CompileModule (JSContext *ctx, const char *input, size_t input_len, const char *filename);
|
CellModule *JS_CompileModule (JSContext *ctx, const char *input, size_t input_len, const char *filename);
|
||||||
|
|
||||||
|
/* Parse source code and return AST as cJSON tree.
|
||||||
|
Caller must call cJSON_Delete() on result. */
|
||||||
|
struct cJSON *JS_ASTTree (const char *source, size_t len, const char *filename);
|
||||||
|
|
||||||
/* Parse source code and return AST as JSON string.
|
/* Parse source code and return AST as JSON string.
|
||||||
Returns malloc'd JSON string (caller must free), or NULL on error.
|
Returns malloc'd JSON string (caller must free), or NULL on error. */
|
||||||
No JSContext needed — pure string transformation. */
|
|
||||||
char *JS_AST (const char *source, size_t len, const char *filename);
|
char *JS_AST (const char *source, size_t len, const char *filename);
|
||||||
|
|
||||||
/* Tokenize source code and return token array as JSON string.
|
/* Tokenize source code and return token array as JSON string.
|
||||||
Returns malloc'd JSON string (caller must free), or NULL on error.
|
Returns malloc'd JSON string (caller must free), or NULL on error. */
|
||||||
No JSContext needed — pure string transformation. */
|
|
||||||
char *JS_Tokenize (const char *source, size_t len, const char *filename);
|
char *JS_Tokenize (const char *source, size_t len, const char *filename);
|
||||||
|
|
||||||
/* Compiled bytecode (context-free, serializable) */
|
/* Compiled bytecode (context-free, serializable) */
|
||||||
typedef struct MachCode MachCode;
|
typedef struct MachCode MachCode;
|
||||||
|
|
||||||
/* Compile AST JSON to context-free MachCode.
|
/* Compile AST cJSON tree to context-free MachCode. */
|
||||||
Returns MachCode* (caller must free with JS_FreeMachCode), or NULL on error. */
|
MachCode *JS_CompileMachTree(struct cJSON *ast);
|
||||||
|
|
||||||
|
/* Compile AST JSON string to context-free MachCode. */
|
||||||
MachCode *JS_CompileMach(const char *ast_json);
|
MachCode *JS_CompileMach(const char *ast_json);
|
||||||
|
|
||||||
/* Free a compiled MachCode tree. */
|
/* Free a compiled MachCode tree. */
|
||||||
void JS_FreeMachCode(MachCode *mc);
|
void JS_FreeMachCode(MachCode *mc);
|
||||||
|
|
||||||
/* Load compiled MachCode into a JSContext, materializing JSValues.
|
/* Load compiled MachCode into a JSContext, materializing JSValues. */
|
||||||
Returns JSCodeRegister* linked and ready for execution. */
|
|
||||||
struct JSCodeRegister *JS_LoadMachCode(JSContext *ctx, MachCode *mc, JSValue env);
|
struct JSCodeRegister *JS_LoadMachCode(JSContext *ctx, MachCode *mc, JSValue env);
|
||||||
|
|
||||||
/* Dump MACH bytecode to stdout for debugging. Takes AST JSON.
|
/* Dump MACH bytecode to stdout. Takes AST cJSON tree. */
|
||||||
Internally compiles, loads, and dumps binary bytecode. */
|
void JS_DumpMachTree (JSContext *ctx, struct cJSON *ast, JSValue env);
|
||||||
|
|
||||||
|
/* Dump MACH bytecode to stdout. Takes AST JSON string. */
|
||||||
void JS_DumpMach (JSContext *ctx, const char *ast_json, JSValue env);
|
void JS_DumpMach (JSContext *ctx, const char *ast_json, JSValue env);
|
||||||
|
|
||||||
/* Compile and execute MACH bytecode. Takes AST JSON.
|
/* Compile and execute MACH bytecode from AST cJSON tree. */
|
||||||
Internally compiles, loads, and executes.
|
JSValue JS_RunMachTree (JSContext *ctx, struct cJSON *ast, JSValue env);
|
||||||
Returns result of execution, or JS_EXCEPTION on error. */
|
|
||||||
|
/* Compile and execute MACH bytecode from AST JSON string. */
|
||||||
JSValue JS_RunMach (JSContext *ctx, const char *ast_json, JSValue env);
|
JSValue JS_RunMach (JSContext *ctx, const char *ast_json, JSValue env);
|
||||||
|
|
||||||
/* Compile AST JSON to MCODE JSON (string-based IR).
|
/* Compile AST cJSON tree to MCODE cJSON tree.
|
||||||
Returns malloc'd JSON string, or NULL on error. Caller must free.
|
Caller must call cJSON_Delete() on result. */
|
||||||
No JSContext needed — pure string transformation. */
|
struct cJSON *JS_McodeTree (struct cJSON *ast);
|
||||||
|
|
||||||
|
/* Compile AST JSON string to MCODE JSON string.
|
||||||
|
Returns malloc'd JSON string, or NULL on error. Caller must free. */
|
||||||
char *JS_Mcode (const char *ast_json);
|
char *JS_Mcode (const char *ast_json);
|
||||||
|
|
||||||
/* Parse and execute MCODE JSON directly via the MCODE interpreter.
|
/* Execute MCODE from cJSON tree. Takes ownership of root. */
|
||||||
|
JSValue JS_CallMcodeTree (JSContext *ctx, struct cJSON *root);
|
||||||
|
|
||||||
|
/* Parse and execute MCODE JSON string.
|
||||||
Returns result of execution, or JS_EXCEPTION on error. */
|
Returns result of execution, or JS_EXCEPTION on error. */
|
||||||
JSValue JS_CallMcode (JSContext *ctx, const char *mcode_json);
|
JSValue JS_CallMcode (JSContext *ctx, const char *mcode_json);
|
||||||
|
|
||||||
|
|||||||
13756
source/runtime.c
Normal file
13756
source/runtime.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -266,11 +266,11 @@ void actor_free(cell_rt *actor)
|
|||||||
|
|
||||||
JSContext *js = actor->context;
|
JSContext *js = actor->context;
|
||||||
|
|
||||||
JS_FreeValue(js, actor->idx_buffer);
|
JS_DeleteGCRef(js, &actor->idx_buffer_ref);
|
||||||
JS_FreeValue(js, actor->message_handle);
|
JS_DeleteGCRef(js, &actor->on_exception_ref);
|
||||||
JS_FreeValue(js, actor->on_exception);
|
JS_DeleteGCRef(js, &actor->message_handle_ref);
|
||||||
JS_FreeValue(js, actor->unneeded);
|
JS_DeleteGCRef(js, &actor->unneeded_ref);
|
||||||
JS_FreeValue(js, actor->actor_sym);
|
JS_DeleteGCRef(js, &actor->actor_sym_ref);
|
||||||
|
|
||||||
for (int i = 0; i < hmlen(actor->timers); i++) {
|
for (int i = 0; i < hmlen(actor->timers); i++) {
|
||||||
JS_FreeValue(js, actor->timers[i].value);
|
JS_FreeValue(js, actor->timers[i].value);
|
||||||
@@ -289,10 +289,8 @@ void actor_free(cell_rt *actor)
|
|||||||
|
|
||||||
arrfree(actor->letters);
|
arrfree(actor->letters);
|
||||||
|
|
||||||
JSRuntime *rt = JS_GetRuntime(js);
|
JS_SetInterruptHandler(js, NULL, NULL);
|
||||||
JS_SetInterruptHandler(rt, NULL, NULL);
|
|
||||||
JS_FreeContext(js);
|
JS_FreeContext(js);
|
||||||
JS_FreeRuntime(rt);
|
|
||||||
free(actor->id);
|
free(actor->id);
|
||||||
|
|
||||||
pthread_mutex_unlock(actor->mutex);
|
pthread_mutex_unlock(actor->mutex);
|
||||||
@@ -419,8 +417,8 @@ uint32_t actor_remove_cb(cell_rt *actor, uint32_t id, uint32_t interval)
|
|||||||
|
|
||||||
actor->disrupt = 1;
|
actor->disrupt = 1;
|
||||||
|
|
||||||
if (!JS_IsNull(actor->unneeded)) {
|
if (!JS_IsNull(actor->unneeded_ref.val)) {
|
||||||
JSValue ret = JS_Call(actor->context, actor->unneeded, JS_NULL, 0, NULL);
|
JSValue ret = JS_Call(actor->context, actor->unneeded_ref.val, JS_NULL, 0, NULL);
|
||||||
uncaught_exception(actor->context, ret);
|
uncaught_exception(actor->context, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -434,14 +432,13 @@ uint32_t actor_remove_cb(cell_rt *actor, uint32_t id, uint32_t interval)
|
|||||||
void actor_unneeded(cell_rt *actor, JSValue fn, double seconds)
|
void actor_unneeded(cell_rt *actor, JSValue fn, double seconds)
|
||||||
{
|
{
|
||||||
if (actor->disrupt) return;
|
if (actor->disrupt) return;
|
||||||
JS_FreeValue(actor->context, actor->unneeded);
|
|
||||||
|
|
||||||
if (!JS_IsFunction(fn)) {
|
if (!JS_IsFunction(fn)) {
|
||||||
actor->unneeded = JS_NULL;
|
actor->unneeded_ref.val = JS_NULL;
|
||||||
goto END;
|
goto END;
|
||||||
}
|
}
|
||||||
|
|
||||||
actor->unneeded = JS_DupValue(actor->context, fn);
|
actor->unneeded_ref.val = fn;
|
||||||
actor->ar_secs = seconds;
|
actor->ar_secs = seconds;
|
||||||
|
|
||||||
END:
|
END:
|
||||||
@@ -493,11 +490,10 @@ cell_rt *create_actor(void *wota)
|
|||||||
actor->heap = mi_heap_new();
|
actor->heap = mi_heap_new();
|
||||||
#endif
|
#endif
|
||||||
actor->init_wota = wota;
|
actor->init_wota = wota;
|
||||||
actor->idx_buffer = JS_NULL;
|
/* GCRef fields are registered after JSContext creation in script_startup.
|
||||||
actor->message_handle = JS_NULL;
|
For now, zero-init from calloc is sufficient (val = 0 = JS_MKVAL(JS_TAG_INT,0),
|
||||||
actor->unneeded = JS_NULL;
|
which is not a pointer so GC-safe). The actual JS_NULL assignment and
|
||||||
actor->on_exception = JS_NULL;
|
JS_AddGCRef happen in script_startup. */
|
||||||
actor->actor_sym = JS_NULL;
|
|
||||||
|
|
||||||
arrsetcap(actor->letters, 5);
|
arrsetcap(actor->letters, 5);
|
||||||
|
|
||||||
@@ -587,7 +583,7 @@ void actor_turn(cell_rt *actor)
|
|||||||
size_t size = blob_length(l.blob_data) / 8; // Convert bits to bytes
|
size_t size = blob_length(l.blob_data) / 8; // Convert bits to bytes
|
||||||
JSValue arg = js_new_blob_stoned_copy(actor->context, (void*)blob_data(l.blob_data), size);
|
JSValue arg = js_new_blob_stoned_copy(actor->context, (void*)blob_data(l.blob_data), size);
|
||||||
blob_destroy(l.blob_data);
|
blob_destroy(l.blob_data);
|
||||||
result = JS_Call(actor->context, actor->message_handle, JS_NULL, 1, &arg);
|
result = JS_Call(actor->context, actor->message_handle_ref.val, JS_NULL, 1, &arg);
|
||||||
uncaught_exception(actor->context, result);
|
uncaught_exception(actor->context, result);
|
||||||
JS_FreeValue(actor->context, arg);
|
JS_FreeValue(actor->context, arg);
|
||||||
} else if (l.type == LETTER_CALLBACK) {
|
} else if (l.type == LETTER_CALLBACK) {
|
||||||
|
|||||||
@@ -118,11 +118,11 @@ void actor_free(cell_rt *actor)
|
|||||||
|
|
||||||
JSContext *js = actor->context;
|
JSContext *js = actor->context;
|
||||||
|
|
||||||
JS_FreeValue(js, actor->idx_buffer);
|
JS_DeleteGCRef(js, &actor->idx_buffer_ref);
|
||||||
JS_FreeValue(js, actor->message_handle);
|
JS_DeleteGCRef(js, &actor->on_exception_ref);
|
||||||
JS_FreeValue(js, actor->on_exception);
|
JS_DeleteGCRef(js, &actor->message_handle_ref);
|
||||||
JS_FreeValue(js, actor->unneeded);
|
JS_DeleteGCRef(js, &actor->unneeded_ref);
|
||||||
JS_FreeValue(js, actor->actor_sym);
|
JS_DeleteGCRef(js, &actor->actor_sym_ref);
|
||||||
|
|
||||||
/* Free timer callbacks stored in actor */
|
/* Free timer callbacks stored in actor */
|
||||||
for (int i = 0; i < hmlen(actor->timers); i++) {
|
for (int i = 0; i < hmlen(actor->timers); i++) {
|
||||||
@@ -142,10 +142,8 @@ void actor_free(cell_rt *actor)
|
|||||||
|
|
||||||
arrfree(actor->letters);
|
arrfree(actor->letters);
|
||||||
|
|
||||||
JSRuntime *rt = JS_GetRuntime(js);
|
JS_SetInterruptHandler(js, NULL, NULL);
|
||||||
JS_SetInterruptHandler(rt, NULL, NULL);
|
|
||||||
JS_FreeContext(js);
|
JS_FreeContext(js);
|
||||||
JS_FreeRuntime(rt);
|
|
||||||
free(actor->id);
|
free(actor->id);
|
||||||
|
|
||||||
free(actor);
|
free(actor);
|
||||||
@@ -157,14 +155,13 @@ void actor_free(cell_rt *actor)
|
|||||||
void actor_unneeded(cell_rt *actor, JSValue fn, double seconds)
|
void actor_unneeded(cell_rt *actor, JSValue fn, double seconds)
|
||||||
{
|
{
|
||||||
if (actor->disrupt) return;
|
if (actor->disrupt) return;
|
||||||
JS_FreeValue(actor->context, actor->unneeded);
|
|
||||||
|
if (!JS_IsFunction(fn)) {
|
||||||
if (!JS_IsFunction(actor->context, fn)) {
|
actor->unneeded_ref.val = JS_NULL;
|
||||||
actor->unneeded = JS_NULL;
|
|
||||||
goto END;
|
goto END;
|
||||||
}
|
}
|
||||||
|
|
||||||
actor->unneeded = JS_DupValue(actor->context, fn);
|
actor->unneeded_ref.val = fn;
|
||||||
actor->ar_secs = seconds;
|
actor->ar_secs = seconds;
|
||||||
|
|
||||||
END:
|
END:
|
||||||
@@ -257,8 +254,8 @@ uint32_t actor_remove_cb(cell_rt *actor, uint32_t id, uint32_t interval)
|
|||||||
|
|
||||||
actor->disrupt = 1;
|
actor->disrupt = 1;
|
||||||
|
|
||||||
if (!JS_IsNull(actor->unneeded)) {
|
if (!JS_IsNull(actor->unneeded_ref.val)) {
|
||||||
JSValue ret = JS_Call(actor->context, actor->unneeded, JS_NULL, 0, NULL);
|
JSValue ret = JS_Call(actor->context, actor->unneeded_ref.val, JS_NULL, 0, NULL);
|
||||||
uncaught_exception(actor->context, ret);
|
uncaught_exception(actor->context, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -328,11 +325,7 @@ cell_rt *create_actor(void *wota)
|
|||||||
{
|
{
|
||||||
cell_rt *actor = calloc(sizeof(*actor), 1);
|
cell_rt *actor = calloc(sizeof(*actor), 1);
|
||||||
actor->init_wota = wota;
|
actor->init_wota = wota;
|
||||||
actor->idx_buffer = JS_NULL;
|
/* GCRef fields are registered after JSContext creation in script_startup. */
|
||||||
actor->message_handle = JS_NULL;
|
|
||||||
actor->unneeded = JS_NULL;
|
|
||||||
actor->on_exception = JS_NULL;
|
|
||||||
actor->actor_sym = JS_NULL;
|
|
||||||
|
|
||||||
arrsetcap(actor->letters, 5);
|
arrsetcap(actor->letters, 5);
|
||||||
|
|
||||||
@@ -408,7 +401,7 @@ void actor_turn(cell_rt *actor)
|
|||||||
size_t size = blob_length(l.blob_data) / 8; // Convert bits to bytes
|
size_t size = blob_length(l.blob_data) / 8; // Convert bits to bytes
|
||||||
JSValue arg = js_new_blob_stoned_copy(actor->context, (void *)blob_data(l.blob_data), size);
|
JSValue arg = js_new_blob_stoned_copy(actor->context, (void *)blob_data(l.blob_data), size);
|
||||||
blob_destroy(l.blob_data);
|
blob_destroy(l.blob_data);
|
||||||
result = JS_Call(actor->context, actor->message_handle, JS_NULL, 1, &arg);
|
result = JS_Call(actor->context, actor->message_handle_ref.val, JS_NULL, 1, &arg);
|
||||||
uncaught_exception(actor->context, result);
|
uncaught_exception(actor->context, result);
|
||||||
JS_FreeValue(actor->context, arg);
|
JS_FreeValue(actor->context, arg);
|
||||||
} else if (l.type == LETTER_CALLBACK) {
|
} else if (l.type == LETTER_CALLBACK) {
|
||||||
|
|||||||
@@ -2393,22 +2393,22 @@ TEST(ast_sem_nested_function_scope) {
|
|||||||
|
|
||||||
TEST(mach_compile_basic) {
|
TEST(mach_compile_basic) {
|
||||||
const char *src = "var x = 1; x = x + 1";
|
const char *src = "var x = 1; x = x + 1";
|
||||||
char *ast_json = JS_AST(src, strlen(src), "<test>");
|
cJSON *ast = JS_ASTTree(src, strlen(src), "<test>");
|
||||||
ASSERT_MSG(ast_json != NULL, "JS_AST returned NULL");
|
ASSERT_MSG(ast != NULL, "JS_ASTTree returned NULL");
|
||||||
MachCode *mc = JS_CompileMach(ast_json);
|
MachCode *mc = JS_CompileMachTree(ast);
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
ASSERT_MSG(mc != NULL, "JS_CompileMach returned NULL");
|
ASSERT_MSG(mc != NULL, "JS_CompileMachTree returned NULL");
|
||||||
JS_FreeMachCode(mc);
|
JS_FreeMachCode(mc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(mach_compile_function) {
|
TEST(mach_compile_function) {
|
||||||
const char *src = "function f(x) { return x + 1 }";
|
const char *src = "function f(x) { return x + 1 }";
|
||||||
char *ast_json = JS_AST(src, strlen(src), "<test>");
|
cJSON *ast = JS_ASTTree(src, strlen(src), "<test>");
|
||||||
ASSERT_MSG(ast_json != NULL, "JS_AST returned NULL");
|
ASSERT_MSG(ast != NULL, "JS_ASTTree returned NULL");
|
||||||
MachCode *mc = JS_CompileMach(ast_json);
|
MachCode *mc = JS_CompileMachTree(ast);
|
||||||
free(ast_json);
|
cJSON_Delete(ast);
|
||||||
ASSERT_MSG(mc != NULL, "JS_CompileMach returned NULL");
|
ASSERT_MSG(mc != NULL, "JS_CompileMachTree returned NULL");
|
||||||
JS_FreeMachCode(mc);
|
JS_FreeMachCode(mc);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
1432
source/tokenize.c
Normal file
1432
source/tokenize.c
Normal file
File diff suppressed because it is too large
Load Diff
657
syntax_suite.ce
Normal file
657
syntax_suite.ce
Normal file
@@ -0,0 +1,657 @@
|
|||||||
|
// Syntax suite: covers every syntactic feature of cell script
|
||||||
|
// Run: ./cell --mach-run syntax_suite.ce
|
||||||
|
|
||||||
|
var passed = 0
|
||||||
|
var failed = 0
|
||||||
|
var error_names = []
|
||||||
|
var error_reasons = []
|
||||||
|
var fail_msg = ""
|
||||||
|
|
||||||
|
for (var _i = 0; _i < 100; _i++) {
|
||||||
|
error_names[] = null
|
||||||
|
error_reasons[] = null
|
||||||
|
}
|
||||||
|
|
||||||
|
var fail = function(msg) {
|
||||||
|
fail_msg = msg
|
||||||
|
disrupt
|
||||||
|
}
|
||||||
|
|
||||||
|
var assert_eq = function(actual, expected, msg) {
|
||||||
|
if (actual != expected) fail(msg + " (got=" + text(actual) + " expected=" + text(expected) + ")")
|
||||||
|
}
|
||||||
|
|
||||||
|
var run = function(name, fn) {
|
||||||
|
fail_msg = ""
|
||||||
|
fn()
|
||||||
|
passed = passed + 1
|
||||||
|
} disruption {
|
||||||
|
error_names[failed] = name
|
||||||
|
error_reasons[failed] = fail_msg == "" ? "disruption" : fail_msg
|
||||||
|
failed = failed + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
var should_disrupt = function(fn) {
|
||||||
|
var caught = false
|
||||||
|
var wrapper = function() {
|
||||||
|
fn()
|
||||||
|
} disruption {
|
||||||
|
caught = true
|
||||||
|
}
|
||||||
|
wrapper()
|
||||||
|
return caught
|
||||||
|
}
|
||||||
|
|
||||||
|
// === LITERALS ===
|
||||||
|
|
||||||
|
run("number literals", function() {
|
||||||
|
assert_eq(42, 42, "integer")
|
||||||
|
assert_eq(3.14 > 3, true, "float")
|
||||||
|
assert_eq(-5, -5, "negative")
|
||||||
|
assert_eq(0, 0, "zero")
|
||||||
|
assert_eq(1e3, 1000, "scientific")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("string literals", function() {
|
||||||
|
assert_eq("hello", "hello", "double quote")
|
||||||
|
assert_eq("", "", "empty string")
|
||||||
|
assert_eq("line1\nline2" != "line1line2", true, "escape sequence")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("template literals", function() {
|
||||||
|
var x = "world"
|
||||||
|
assert_eq(`hello ${x}`, "hello world", "interpolation")
|
||||||
|
assert_eq(`${1 + 2}`, "3", "expression interpolation")
|
||||||
|
assert_eq(`plain`, "plain", "no interpolation")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("boolean literals", function() {
|
||||||
|
assert_eq(true, true, "true")
|
||||||
|
assert_eq(false, false, "false")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("null literal", function() {
|
||||||
|
assert_eq(null, null, "null")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("array literal", function() {
|
||||||
|
var a = [1, 2, 3]
|
||||||
|
assert_eq(length(a), 3, "array length")
|
||||||
|
assert_eq(a[0], 1, "first element")
|
||||||
|
var e = []
|
||||||
|
assert_eq(length(e), 0, "empty array")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("object literal", function() {
|
||||||
|
var o = {a: 1, b: "two"}
|
||||||
|
assert_eq(o.a, 1, "object prop")
|
||||||
|
var e = {}
|
||||||
|
assert_eq(e.x, null, "empty object missing prop")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("regex literal", function() {
|
||||||
|
var r = /\d+/
|
||||||
|
var result = extract("abc123", r)
|
||||||
|
assert_eq(result[0], "123", "regex match")
|
||||||
|
var ri = /hello/i
|
||||||
|
var result2 = extract("Hello", ri)
|
||||||
|
assert_eq(result2[0], "Hello", "regex flags")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === DECLARATIONS ===
|
||||||
|
|
||||||
|
run("var declaration", function() {
|
||||||
|
var x = 5
|
||||||
|
assert_eq(x, 5, "var init")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("var uninitialized", function() {
|
||||||
|
var x
|
||||||
|
assert_eq(x, null, "var defaults to null")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("var multiple declaration", function() {
|
||||||
|
var a = 1, b = 2, c = 3
|
||||||
|
assert_eq(a + b + c, 6, "multi var")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("def declaration", function() {
|
||||||
|
def x = 42
|
||||||
|
assert_eq(x, 42, "def const")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === ARITHMETIC OPERATORS ===
|
||||||
|
|
||||||
|
run("arithmetic operators", function() {
|
||||||
|
assert_eq(2 + 3, 5, "add")
|
||||||
|
assert_eq(5 - 3, 2, "sub")
|
||||||
|
assert_eq(3 * 4, 12, "mul")
|
||||||
|
assert_eq(12 / 4, 3, "div")
|
||||||
|
assert_eq(10 % 3, 1, "mod")
|
||||||
|
assert_eq(2 ** 3, 8, "exp")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === COMPARISON OPERATORS ===
|
||||||
|
|
||||||
|
run("comparison operators", function() {
|
||||||
|
assert_eq(5 == 5, true, "eq")
|
||||||
|
assert_eq(5 != 6, true, "neq")
|
||||||
|
assert_eq(3 < 5, true, "lt")
|
||||||
|
assert_eq(5 > 3, true, "gt")
|
||||||
|
assert_eq(3 <= 3, true, "lte")
|
||||||
|
assert_eq(5 >= 5, true, "gte")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === LOGICAL OPERATORS ===
|
||||||
|
|
||||||
|
run("logical operators", function() {
|
||||||
|
assert_eq(true && true, true, "and")
|
||||||
|
assert_eq(true && false, false, "and false")
|
||||||
|
assert_eq(false || true, true, "or")
|
||||||
|
assert_eq(false || false, false, "or false")
|
||||||
|
assert_eq(!true, false, "not")
|
||||||
|
assert_eq(!false, true, "not false")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("short circuit", function() {
|
||||||
|
var called = false
|
||||||
|
var fn = function() { called = true; return true }
|
||||||
|
var r = false && fn()
|
||||||
|
assert_eq(called, false, "and short circuit")
|
||||||
|
r = true || fn()
|
||||||
|
assert_eq(called, false, "or short circuit")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === BITWISE OPERATORS ===
|
||||||
|
|
||||||
|
run("bitwise operators", function() {
|
||||||
|
assert_eq(5 & 3, 1, "and")
|
||||||
|
assert_eq(5 | 3, 7, "or")
|
||||||
|
assert_eq(5 ^ 3, 6, "xor")
|
||||||
|
assert_eq(~0, -1, "not")
|
||||||
|
assert_eq(1 << 3, 8, "lshift")
|
||||||
|
assert_eq(8 >> 3, 1, "rshift")
|
||||||
|
assert_eq(-1 >>> 1, 2147483647, "unsigned rshift")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === UNARY OPERATORS ===
|
||||||
|
|
||||||
|
run("unary operators", function() {
|
||||||
|
assert_eq(+5, 5, "unary plus")
|
||||||
|
assert_eq(-5, -5, "unary minus")
|
||||||
|
assert_eq(-(-5), 5, "double negate")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("increment decrement", function() {
|
||||||
|
var x = 5
|
||||||
|
assert_eq(x++, 5, "postfix inc returns old")
|
||||||
|
assert_eq(x, 6, "postfix inc side effect")
|
||||||
|
x = 5
|
||||||
|
assert_eq(++x, 6, "prefix inc returns new")
|
||||||
|
x = 5
|
||||||
|
assert_eq(x--, 5, "postfix dec returns old")
|
||||||
|
assert_eq(x, 4, "postfix dec side effect")
|
||||||
|
x = 5
|
||||||
|
assert_eq(--x, 4, "prefix dec returns new")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === COMPOUND ASSIGNMENT ===
|
||||||
|
|
||||||
|
run("compound assignment", function() {
|
||||||
|
var x = 10
|
||||||
|
x += 3; assert_eq(x, 13, "+=")
|
||||||
|
x -= 3; assert_eq(x, 10, "-=")
|
||||||
|
x *= 2; assert_eq(x, 20, "*=")
|
||||||
|
x /= 4; assert_eq(x, 5, "/=")
|
||||||
|
x %= 3; assert_eq(x, 2, "%=")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === TERNARY OPERATOR ===
|
||||||
|
|
||||||
|
run("ternary operator", function() {
|
||||||
|
var a = true ? 1 : 2
|
||||||
|
assert_eq(a, 1, "ternary true")
|
||||||
|
var b = false ? 1 : 2
|
||||||
|
assert_eq(b, 2, "ternary false")
|
||||||
|
var c = true ? (false ? 1 : 2) : 3
|
||||||
|
assert_eq(c, 2, "ternary nested")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === COMMA OPERATOR ===
|
||||||
|
|
||||||
|
run("comma operator", function() {
|
||||||
|
var x = (1, 2, 3)
|
||||||
|
assert_eq(x, 3, "comma returns last")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === IN OPERATOR ===
|
||||||
|
|
||||||
|
run("in operator", function() {
|
||||||
|
var o = {a: 1}
|
||||||
|
assert_eq("a" in o, true, "key exists")
|
||||||
|
assert_eq("b" in o, false, "key missing")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === DELETE OPERATOR ===
|
||||||
|
|
||||||
|
run("delete operator", function() {
|
||||||
|
var o = {a: 1, b: 2}
|
||||||
|
delete o.a
|
||||||
|
assert_eq("a" in o, false, "delete removes key")
|
||||||
|
assert_eq(o.b, 2, "delete leaves others")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === PROPERTY ACCESS ===
|
||||||
|
|
||||||
|
run("dot access", function() {
|
||||||
|
var o = {x: 10}
|
||||||
|
assert_eq(o.x, 10, "dot read")
|
||||||
|
o.x = 20
|
||||||
|
assert_eq(o.x, 20, "dot write")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("bracket access", function() {
|
||||||
|
var o = {x: 10}
|
||||||
|
assert_eq(o["x"], 10, "bracket read")
|
||||||
|
var key = "x"
|
||||||
|
assert_eq(o[key], 10, "computed bracket")
|
||||||
|
o["y"] = 20
|
||||||
|
assert_eq(o.y, 20, "bracket write")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("object-as-key", function() {
|
||||||
|
var k = {}
|
||||||
|
var o = {}
|
||||||
|
o[k] = 42
|
||||||
|
assert_eq(o[k], 42, "object key set/get")
|
||||||
|
assert_eq(o[{}], null, "new object is different key")
|
||||||
|
assert_eq(k in o, true, "object key in")
|
||||||
|
delete o[k]
|
||||||
|
assert_eq(k in o, false, "object key delete")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("chained access", function() {
|
||||||
|
var d = {a: {b: [1, {c: 99}]}}
|
||||||
|
assert_eq(d.a.b[1].c, 99, "mixed chain")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === ARRAY PUSH/POP SYNTAX ===
|
||||||
|
|
||||||
|
run("array push pop", function() {
|
||||||
|
var a = [1, 2]
|
||||||
|
a[] = 3
|
||||||
|
assert_eq(length(a), 3, "push length")
|
||||||
|
assert_eq(a[2], 3, "push value")
|
||||||
|
var v = a[]
|
||||||
|
assert_eq(v, 3, "pop value")
|
||||||
|
assert_eq(length(a), 2, "pop length")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === CONTROL FLOW: IF/ELSE ===
|
||||||
|
|
||||||
|
run("if else", function() {
|
||||||
|
var x = 0
|
||||||
|
if (true) x = 1
|
||||||
|
assert_eq(x, 1, "if true")
|
||||||
|
if (false) x = 2 else x = 3
|
||||||
|
assert_eq(x, 3, "if else")
|
||||||
|
if (false) x = 4
|
||||||
|
else if (true) x = 5
|
||||||
|
else x = 6
|
||||||
|
assert_eq(x, 5, "else if")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === CONTROL FLOW: WHILE ===
|
||||||
|
|
||||||
|
run("while loop", function() {
|
||||||
|
var i = 0
|
||||||
|
while (i < 5) i++
|
||||||
|
assert_eq(i, 5, "while basic")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("while break continue", function() {
|
||||||
|
var i = 0
|
||||||
|
while (true) {
|
||||||
|
if (i >= 3) break
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
assert_eq(i, 3, "while break")
|
||||||
|
var sum = 0
|
||||||
|
i = 0
|
||||||
|
while (i < 5) {
|
||||||
|
i++
|
||||||
|
if (i % 2 == 0) continue
|
||||||
|
sum += i
|
||||||
|
}
|
||||||
|
assert_eq(sum, 9, "while continue")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === CONTROL FLOW: FOR ===
|
||||||
|
|
||||||
|
run("for loop", function() {
|
||||||
|
var sum = 0
|
||||||
|
for (var i = 0; i < 5; i++) sum += i
|
||||||
|
assert_eq(sum, 10, "for basic")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("for break continue", function() {
|
||||||
|
var sum = 0
|
||||||
|
for (var i = 0; i < 10; i++) {
|
||||||
|
if (i == 5) break
|
||||||
|
sum += i
|
||||||
|
}
|
||||||
|
assert_eq(sum, 10, "for break")
|
||||||
|
sum = 0
|
||||||
|
for (var i = 0; i < 5; i++) {
|
||||||
|
if (i % 2 == 0) continue
|
||||||
|
sum += i
|
||||||
|
}
|
||||||
|
assert_eq(sum, 4, "for continue")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("nested for", function() {
|
||||||
|
var sum = 0
|
||||||
|
for (var i = 0; i < 3; i++)
|
||||||
|
for (var j = 0; j < 3; j++)
|
||||||
|
sum++
|
||||||
|
assert_eq(sum, 9, "nested for")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === BLOCK SCOPING ===
|
||||||
|
|
||||||
|
run("block scoping", function() {
|
||||||
|
var x = 1
|
||||||
|
{
|
||||||
|
var x = 2
|
||||||
|
assert_eq(x, 2, "inner block")
|
||||||
|
}
|
||||||
|
assert_eq(x, 1, "outer preserved")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("for iterator scope", function() {
|
||||||
|
for (var i = 0; i < 1; i++) {}
|
||||||
|
assert_eq(should_disrupt(function() { var y = i }), true, "for var does not leak")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === FUNCTIONS ===
|
||||||
|
|
||||||
|
run("function expression", function() {
|
||||||
|
var fn = function(a, b) { return a + b }
|
||||||
|
assert_eq(fn(2, 3), 5, "basic call")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("arrow function", function() {
|
||||||
|
var double = x => x * 2
|
||||||
|
assert_eq(double(5), 10, "arrow single param")
|
||||||
|
var add = (a, b) => a + b
|
||||||
|
assert_eq(add(2, 3), 5, "arrow multi param")
|
||||||
|
var block = x => {
|
||||||
|
var y = x * 2
|
||||||
|
return y + 1
|
||||||
|
}
|
||||||
|
assert_eq(block(5), 11, "arrow block body")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("function no return", function() {
|
||||||
|
var fn = function() { var x = 1 }
|
||||||
|
assert_eq(fn(), null, "no return gives null")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("function early return", function() {
|
||||||
|
var fn = function() { return 1; return 2 }
|
||||||
|
assert_eq(fn(), 1, "early return")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("extra and missing args", function() {
|
||||||
|
var fn = function(a, b) { return a + b }
|
||||||
|
assert_eq(fn(1, 2, 3), 3, "extra args ignored")
|
||||||
|
var fn2 = function(a, b) { return a }
|
||||||
|
assert_eq(fn2(1), 1, "missing args ok")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("iife", function() {
|
||||||
|
var r = (function(x) { return x * 2 })(21)
|
||||||
|
assert_eq(r, 42, "immediately invoked")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === CLOSURES ===
|
||||||
|
|
||||||
|
run("closure", function() {
|
||||||
|
var make = function(x) {
|
||||||
|
return function(y) { return x + y }
|
||||||
|
}
|
||||||
|
var add5 = make(5)
|
||||||
|
assert_eq(add5(3), 8, "closure captures")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("closure mutation", function() {
|
||||||
|
var counter = function() {
|
||||||
|
var n = 0
|
||||||
|
return function() { n = n + 1; return n }
|
||||||
|
}
|
||||||
|
var c = counter()
|
||||||
|
assert_eq(c(), 1, "first")
|
||||||
|
assert_eq(c(), 2, "second")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === RECURSION ===
|
||||||
|
|
||||||
|
run("recursion", function() {
|
||||||
|
var fact = function(n) {
|
||||||
|
if (n <= 1) return 1
|
||||||
|
return n * fact(n - 1)
|
||||||
|
}
|
||||||
|
assert_eq(fact(5), 120, "factorial")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === THIS BINDING ===
|
||||||
|
|
||||||
|
run("this binding", function() {
|
||||||
|
var obj = {
|
||||||
|
val: 10,
|
||||||
|
get: function() { return this.val }
|
||||||
|
}
|
||||||
|
assert_eq(obj.get(), 10, "method this")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === DISRUPTION ===
|
||||||
|
|
||||||
|
run("disrupt keyword", function() {
|
||||||
|
assert_eq(should_disrupt(function() { disrupt }), true, "bare disrupt")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("disruption handler", function() {
|
||||||
|
var x = 0
|
||||||
|
var fn = function() { x = 1 } disruption { x = 2 }
|
||||||
|
fn()
|
||||||
|
assert_eq(x, 1, "no disruption path")
|
||||||
|
var fn2 = function() { disrupt } disruption { x = 3 }
|
||||||
|
fn2()
|
||||||
|
assert_eq(x, 3, "disruption caught")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("disruption re-raise", function() {
|
||||||
|
var outer_caught = false
|
||||||
|
var outer = function() {
|
||||||
|
var inner = function() { disrupt } disruption { disrupt }
|
||||||
|
inner()
|
||||||
|
} disruption {
|
||||||
|
outer_caught = true
|
||||||
|
}
|
||||||
|
outer()
|
||||||
|
assert_eq(outer_caught, true, "re-raise propagates")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === PROTOTYPAL INHERITANCE ===
|
||||||
|
|
||||||
|
run("meme and proto", function() {
|
||||||
|
var parent = {x: 10}
|
||||||
|
var child = meme(parent)
|
||||||
|
assert_eq(child.x, 10, "inherited prop")
|
||||||
|
assert_eq(proto(child), parent, "proto returns parent")
|
||||||
|
child.x = 20
|
||||||
|
assert_eq(parent.x, 10, "override does not mutate parent")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("meme with mixins", function() {
|
||||||
|
var p = {a: 1}
|
||||||
|
var m1 = {b: 2}
|
||||||
|
var m2 = {c: 3}
|
||||||
|
var child = meme(p, [m1, m2])
|
||||||
|
assert_eq(child.a, 1, "parent prop")
|
||||||
|
assert_eq(child.b, 2, "mixin1")
|
||||||
|
assert_eq(child.c, 3, "mixin2")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === STONE (FREEZE) ===
|
||||||
|
|
||||||
|
run("stone", function() {
|
||||||
|
var o = {x: 1}
|
||||||
|
assert_eq(is_stone(o), false, "not frozen")
|
||||||
|
stone(o)
|
||||||
|
assert_eq(is_stone(o), true, "frozen")
|
||||||
|
assert_eq(should_disrupt(function() { o.x = 2 }), true, "write disrupts")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === FUNCTION PROXY ===
|
||||||
|
|
||||||
|
run("function proxy", function() {
|
||||||
|
var proxy = function(name, args) {
|
||||||
|
return `${name}:${length(args)}`
|
||||||
|
}
|
||||||
|
assert_eq(proxy.hello(), "hello:0", "proxy dot call")
|
||||||
|
assert_eq(proxy.add(1, 2), "add:2", "proxy with args")
|
||||||
|
assert_eq(proxy["method"](), "method:0", "proxy bracket call")
|
||||||
|
var m = "dynamic"
|
||||||
|
assert_eq(proxy[m](), "dynamic:0", "proxy computed name")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("non-proxy function prop access disrupts", function() {
|
||||||
|
var fn = function() { return 1 }
|
||||||
|
assert_eq(should_disrupt(function() { var x = fn.foo }), true, "prop read disrupts")
|
||||||
|
assert_eq(should_disrupt(function() { fn.foo = 1 }), true, "prop write disrupts")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === TYPE CHECKING ===
|
||||||
|
|
||||||
|
run("is_* functions", function() {
|
||||||
|
assert_eq(is_number(42), true, "is_number")
|
||||||
|
assert_eq(is_text("hi"), true, "is_text")
|
||||||
|
assert_eq(is_logical(true), true, "is_logical")
|
||||||
|
assert_eq(is_object({}), true, "is_object")
|
||||||
|
assert_eq(is_array([]), true, "is_array")
|
||||||
|
assert_eq(is_function(function(){}), true, "is_function")
|
||||||
|
assert_eq(is_null(null), true, "is_null")
|
||||||
|
assert_eq(is_object([]), false, "array not object")
|
||||||
|
assert_eq(is_array({}), false, "object not array")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === TRUTHINESS / FALSINESS ===
|
||||||
|
|
||||||
|
run("falsy values", function() {
|
||||||
|
if (false) fail("false")
|
||||||
|
if (0) fail("0")
|
||||||
|
if ("") fail("empty string")
|
||||||
|
if (null) fail("null")
|
||||||
|
assert_eq(true, true, "all falsy passed")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("truthy values", function() {
|
||||||
|
if (!1) fail("1")
|
||||||
|
if (!"hi") fail("string")
|
||||||
|
if (!{}) fail("object")
|
||||||
|
if (![]) fail("array")
|
||||||
|
if (!true) fail("true")
|
||||||
|
assert_eq(true, true, "all truthy passed")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === VARIABLE SHADOWING ===
|
||||||
|
|
||||||
|
run("variable shadowing", function() {
|
||||||
|
var x = 10
|
||||||
|
var fn = function() {
|
||||||
|
var x = 20
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
assert_eq(fn(), 20, "inner shadows")
|
||||||
|
assert_eq(x, 10, "outer unchanged")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === OPERATOR PRECEDENCE ===
|
||||||
|
|
||||||
|
run("precedence", function() {
|
||||||
|
assert_eq(2 + 3 * 4, 14, "mul before add")
|
||||||
|
assert_eq((2 + 3) * 4, 20, "parens override")
|
||||||
|
assert_eq(-2 * 3, -6, "unary before mul")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === CURRYING / HIGHER-ORDER ===
|
||||||
|
|
||||||
|
run("curried function", function() {
|
||||||
|
var f = function(a) {
|
||||||
|
return function(b) {
|
||||||
|
return function(c) { return a + b + c }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq(f(1)(2)(3), 6, "triple curry")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === SELF-REFERENCING STRUCTURES ===
|
||||||
|
|
||||||
|
run("self-referencing object", function() {
|
||||||
|
var o = {name: "root"}
|
||||||
|
o.self = o
|
||||||
|
assert_eq(o.self.self.name, "root", "cycle access")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === IDENTIFIER ? AND ! ===
|
||||||
|
|
||||||
|
run("question mark in identifier", function() {
|
||||||
|
var nil? = (x) => x == null
|
||||||
|
assert_eq(nil?(null), true, "nil? null")
|
||||||
|
assert_eq(nil?(42), false, "nil? 42")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("bang in identifier", function() {
|
||||||
|
var set! = (x) => x + 1
|
||||||
|
assert_eq(set!(5), 6, "set! call")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("question mark mid identifier", function() {
|
||||||
|
var is?valid = (x) => x > 0
|
||||||
|
assert_eq(is?valid(3), true, "is?valid true")
|
||||||
|
assert_eq(is?valid(-1), false, "is?valid false")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("bang mid identifier", function() {
|
||||||
|
var do!stuff = () => 42
|
||||||
|
assert_eq(do!stuff(), 42, "do!stuff call")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("ternary after question ident", function() {
|
||||||
|
var nil? = (x) => x == null
|
||||||
|
var a = nil?(null) ? "yes" : "no"
|
||||||
|
assert_eq(a, "yes", "ternary true branch")
|
||||||
|
var b = nil?(42) ? "yes" : "no"
|
||||||
|
assert_eq(b, "no", "ternary false branch")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("bang not confused with logical not", function() {
|
||||||
|
assert_eq(!true, false, "logical not true")
|
||||||
|
assert_eq(!false, true, "logical not false")
|
||||||
|
})
|
||||||
|
|
||||||
|
run("inequality not confused with bang ident", function() {
|
||||||
|
assert_eq(1 != 2, true, "inequality true")
|
||||||
|
assert_eq(1 != 1, false, "inequality false")
|
||||||
|
})
|
||||||
|
|
||||||
|
// === SUMMARY ===
|
||||||
|
|
||||||
|
print(text(passed) + " passed, " + text(failed) + " failed out of " + text(passed + failed))
|
||||||
|
if (failed > 0) {
|
||||||
|
print("")
|
||||||
|
for (var _j = 0; _j < failed; _j++) {
|
||||||
|
print(" FAIL " + error_names[_j] + ": " + error_reasons[_j])
|
||||||
|
}
|
||||||
|
}
|
||||||
3558
vm_suite.ce
Normal file
3558
vm_suite.ce
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user