fix fd.c bugs

This commit is contained in:
2026-02-10 14:21:49 -06:00
parent 54673e4a04
commit fe5dc6ecc9
10 changed files with 815 additions and 794 deletions

18
fd.c
View File

@@ -504,7 +504,7 @@ JSC_SCALL(fd_readdir,
ret = JS_NewArray(js);
do {
if (strcmp(ffd.cFileName, ".") == 0 || strcmp(ffd.cFileName, "..") == 0) continue;
JS_ArrayPush(js, ret,JS_NewString(js, ffd.cFileName));
JS_ArrayPush(js, &ret, JS_NewString(js, ffd.cFileName));
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
}
@@ -516,7 +516,7 @@ JSC_SCALL(fd_readdir,
ret = JS_NewArray(js);
while ((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
JS_ArrayPush(js, ret, JS_NewString(js, dir->d_name));
JS_ArrayPush(js, &ret, JS_NewString(js, dir->d_name));
}
closedir(d);
} else {
@@ -565,18 +565,22 @@ JSC_CCALL(fd_slurpwrite,
if (!str) return JS_EXCEPTION;
int fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
ret = JS_ThrowInternalError(js, "open failed for %s: %s", str, strerror(errno));
JS_FreeCString(js, str);
return JS_ThrowInternalError(js, "open failed for %s: %s", str, strerror(errno));
return ret;
}
ssize_t written = write(fd, data, len);
close(fd);
if (written != (ssize_t)len) {
ret = JS_ThrowInternalError(js, "write failed for %s: %s", str, strerror(errno));
JS_FreeCString(js, str);
return ret;
}
JS_FreeCString(js, str);
if (written != (ssize_t)len)
return JS_ThrowInternalError(js, "write failed for %s: %s", str, strerror(errno));
return JS_NULL;
)

2
fd.cm
View File

@@ -1,4 +1,4 @@
var fd = this
var fd = native
var wildstar = use('wildstar')
function last_pos(str, sep) {

View File

@@ -56,9 +56,13 @@ var packages_path = shop_path ? shop_path + '/packages' : null
var use_cache = {}
use_cache['core/os'] = os
// Extra env properties added as engine initializes (log, runtime fns, etc.)
var core_extras = {}
// Load a core module from the file system
function use_core(path) {
var cache_key = 'core/' + path
var env = null
if (use_cache[cache_key])
return use_cache[cache_key]
@@ -67,10 +71,15 @@ function use_core(path) {
var script = null
var ast = null
// Build env: merge core_extras, include C embed as 'native' if available
env = {use: use_core}
arrfor(array(core_extras), function(k) { env[k] = core_extras[k] })
if (sym) env.native = sym
// Check for pre-compiled .mach file first
var mach_path = core_path + '/' + path + '.mach'
if (fd.is_file(mach_path)) {
result = mach_load(fd.slurp(mach_path), {use: use_core})
result = mach_load(fd.slurp(mach_path), env)
use_cache[cache_key] = result
return result
}
@@ -80,7 +89,7 @@ function use_core(path) {
if (fd.is_file(file_path)) {
script = text(fd.slurp(file_path))
ast = analyze(script, file_path)
result = run_ast_fn('core:' + path, ast, {use: use_core})
result = run_ast_fn('core:' + path, ast, env)
use_cache[cache_key] = result
return result
}
@@ -90,6 +99,9 @@ function use_core(path) {
return sym
}
// Load full modules via use_core (extends C embeds with .cm additions, and caches)
fd = use_core('fd')
use_core('js')
var blob = use_core('blob')
function actor() {
@@ -223,6 +235,9 @@ var runtime_env = {
sequence: sequence
}
// Make runtime functions available to modules loaded via use_core
arrfor(array(runtime_env), function(k) { core_extras[k] = runtime_env[k] })
// Pass to os for shop to access
os.runtime_env = runtime_env
@@ -845,7 +860,11 @@ $_.clock(_ => {
}
var pkg = file_info ? file_info.package : null
env.use = function(path) { return shop.use(path, pkg) }
env.use = function(path) {
var ck = 'core/' + path
if (use_cache[ck]) return use_cache[ck]
return shop.use(path, pkg)
}
env.args = _cell.args.arg
env.log = log

Binary file not shown.

View File

@@ -453,15 +453,15 @@ function make_use_fn_code(pkg_arg) {
// for script forms, path is the canonical path of the module
var script_form = function(path, script, pkg, inject) {
var pkg_arg = pkg ? `'${pkg}'` : 'null'
var pkg_arg = pkg ? "'" + pkg + "'" : 'null'
var binds = inject_bindings_code(inject)
var fn = `(function setup_module(args, use, env){
def arg = args;
def PACKAGE = ${pkg_arg};
${binds}
${script}
})`
var fn = "(function setup_module(args, use, env){\n" +
"def arg = args;\n" +
"def PACKAGE = " + pkg_arg + ";\n" +
binds + "\n" +
script + "\n" +
"})"
return fn
}

View File

@@ -3125,13 +3125,18 @@ JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code,
goto disrupt;
} else {
/* Record method call: get property, call with this=obj */
if (JS_IsNull(frame->slots[base])) {
JS_ThrowTypeError(ctx, "cannot read properties of null");
JS_PopGCRef(ctx, &key_ref);
goto disrupt;
}
JSValue method = JS_GetProperty(ctx, frame->slots[base], key_ref.val);
frame = (JSFrameRegister *)JS_VALUE_GET_PTR(frame_ref.val);
if (JS_IsException(method)) { JS_PopGCRef(ctx, &key_ref); goto disrupt; }
if (!JS_IsFunction(method)) {
frame->slots[base] = JS_NULL;
JS_ThrowTypeError(ctx, "not a function");
JS_PopGCRef(ctx, &key_ref);
break;
goto disrupt;
}
JSFunction *fn = JS_VALUE_GET_FUNCTION(method);
if (fn->kind == JS_FUNC_KIND_C) {

View File

@@ -9923,7 +9923,7 @@ static JSValue js_blob_constructor (JSContext *ctx, JSValue this_val, int argc,
}
}
/* blob(blob, from, to) - copy from another blob */
else if (argc >= 1 && JS_IsObject (argv[0])) {
else if (argc >= 1 && JS_IsObject (argv[0]) && !JS_IsText (argv[0])) {
blob *src = js_get_blob (ctx, argv[0]);
if (!src)
return JS_ThrowTypeError (ctx,
@@ -9941,9 +9941,7 @@ static JSValue js_blob_constructor (JSContext *ctx, JSValue this_val, int argc,
bd = blob_new_from_blob (src, (size_t)from, (size_t)to);
}
/* blob(text) - create blob from UTF-8 string */
else if (argc == 1
&& (JS_VALUE_GET_TAG (argv[0]) == JS_TAG_STRING
|| JS_VALUE_GET_TAG (argv[0]) == JS_TAG_STRING_IMM)) {
else if (argc == 1 && JS_IsText (argv[0])) {
const char *str = JS_ToCString (ctx, argv[0]);
if (!str) return JS_EXCEPTION;
size_t len = strlen (str);

31
test.ce
View File

@@ -304,9 +304,12 @@ function run_tests(package_name, specific_test) {
}
var _load_file = null
var load_error = false
var err_entry = null
for (i = 0; i < length(test_files); i++) {
f = test_files[i]
mod_path = text(f, 0, -3) // remove .cm
load_error = false
file_result = {
name: f,
@@ -355,11 +358,11 @@ function run_tests(package_name, specific_test) {
var ret = t.fn()
if (is_text(ret)) {
_test_error = Error(ret)
disrupt
} else if (ret && (is_text(ret.message) || is_proto(ret, Error))) {
_test_error = ret
disrupt
} else if (ret && is_text(ret.message)) {
_test_error = ret.message
disrupt
}
test_entry.status = "passed"
@@ -400,23 +403,15 @@ function run_tests(package_name, specific_test) {
}
} disruption {
var test_entry = {
package: pkg_result.package,
test: "load_module",
status: "failed",
duration_ns: 0,
error: { message: `Error loading module` }
}
log.console(` Error loading ${f}`)
push(file_result.tests, test_entry)
pkg_result.failed++
file_result.failed++
pkg_result.total++
if (gc_after_each_test) {
dbg.gc()
}
load_error = true
}
_load_file()
if (load_error) {
log.console(" Error loading " + f)
pkg_result.failed = pkg_result.failed + 1
file_result.failed = file_result.failed + 1
pkg_result.total = pkg_result.total + 1
}
push(pkg_result.files, file_result)
}
return pkg_result

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
// epoch = 0000-01-01 00:00:00 +0000
var time = this
var time = native
var now = time.now
var computer_zone = time.computer_zone