diff --git a/benches/micro_ops.cm b/benches/micro_ops.cm index 408c8313..400b2050 100644 --- a/benches/micro_ops.cm +++ b/benches/micro_ops.cm @@ -204,7 +204,7 @@ return { for (var j = 0; j < n; j++) { var a = [] for (var i = 0; i < 256; i++) a.push(i) - x = (x + a.length) | 0 + x = (x + length(a)) | 0 } return blackhole(sink, x) }, @@ -216,7 +216,7 @@ return { for (var j = 0; j < n; j++) { var s = "" for (var i = 0; i < 16; i++) s = s + "x" - x = (x + s.length) | 0 + x = (x + length(s)) | 0 } return blackhole(sink, x) }, diff --git a/benchmarks/wota_nota_json.ce b/benchmarks/wota_nota_json.ce index e25f0841..4564bd56 100644 --- a/benchmarks/wota_nota_json.ce +++ b/benchmarks/wota_nota_json.ce @@ -50,7 +50,6 @@ def libraries = [ decode: json.decode, // json produces a JS string. We'll measure its UTF-16 code unit length // as a rough "size". Alternatively, you could convert to UTF-8 for - // a more accurate byte size. Here we just use `string.length`. getSize(encodedStr) { return length(encodedStr); } diff --git a/build.cm b/build.cm index 5cffec7b..a73812e6 100644 --- a/build.cm +++ b/build.cm @@ -85,7 +85,7 @@ function ensure_dir(path) { if (fd.stat(path).isDirectory) return var parts = array(path, '/') var current = starts_with(path, '/') ? '/' : '' - for (var i = 0; i < parts.length; i++) { + for (var i = 0; i < length(parts); i++) { if (parts[i] == '') continue current += parts[i] + '/' if (!fd.stat(current).isDirectory) { @@ -223,7 +223,7 @@ function compute_link_key(objects, ldflags, target_ldflags, target, cc) { Build.build_dynamic = function(pkg, target = Build.detect_host_target(), buildtype = 'release') { var objects = Build.build_package(pkg, target, true, buildtype) // exclude main.c - if (objects.length == 0) { + if (length(objects) == 0) { log.console('No C files in ' + pkg) return null } @@ -382,7 +382,7 @@ Build.build_static = function(packages, target = Build.detect_host_target(), out } }) - if (all_objects.length == 0) { + if (length(all_objects) == 0) { throw Error('No object files to link') } diff --git a/cellfs.cm b/cellfs.cm index cb72e198..923b8798 100644 --- a/cellfs.cm +++ b/cellfs.cm @@ -174,7 +174,7 @@ function mount(source, name) { // Unmount function unmount(name_or_source) { - for (var i = 0; i < mounts.length; i++) { + for (var i = 0; i < length(mounts); i++) { if (mounts[i].name == name_or_source || mounts[i].source == name_or_source) { mounts.splice(i, 1) return @@ -342,7 +342,7 @@ function enumerate(path, recurse) { } else if (res.mount.type == 'qop') { var all = res.mount.handle.list() var prefix = res.path ? res.path + "/" : "" - var prefix_len = prefix.length + var prefix_len = length(prefix) // Use a set to avoid duplicates if we are simulating directories var seen = {} @@ -350,7 +350,7 @@ function enumerate(path, recurse) { arrfor(all, function(p) { if (starts_with(p, prefix)) { var rel = text(p, prefix_len) - if (rel.length == 0) return + if (length(rel) == 0) return if (!recurse) { var slash = search(rel, '/') @@ -430,12 +430,12 @@ function globfs(globs, dir) { } else if (res.mount.type == 'qop') { var all = res.mount.handle.list() var prefix = res.path ? res.path + "/" : "" - var prefix_len = prefix.length + var prefix_len = length(prefix) arrfor(all, function(p) { if (starts_with(p, prefix)) { var rel = text(p, prefix_len) - if (rel.length == 0) return + if (length(rel) == 0) return if (!check_neg(rel) && check_pos(rel)) { results.push(rel) diff --git a/internal/engine.cm b/internal/engine.cm index 5c76b3b5..9d5b3980 100644 --- a/internal/engine.cm +++ b/internal/engine.cm @@ -453,7 +453,7 @@ $_.start = function start(cb, program, ...args) { if (!program) return var id = guid() - if (args.length == 1 && is_array(args[0])) args = args[0] + if (length(args) == 1 && is_array(args[0])) args = args[0] var startup = { id, overling: $_.self, @@ -540,7 +540,7 @@ function actor_send(actor, message) { // message to actor in same flock if (actor[ACTORDATA].id && actor_mod.mailbox_exist(actor[ACTORDATA].id)) { var wota_blob = wota.encode(message) -// log.console(`sending wota blob of ${wota_blob.length/8} bytes`) +// log.console(`sending wota blob of ${length(wota_blob)/8} bytes`) actor_mod.mailbox_push(actor[ACTORDATA].id, wota_blob) return } @@ -578,7 +578,7 @@ var need_stop = false // if we've been flagged to stop, bail out before doing anything if (need_stop) { disrupt() - message_queue.length = 0 + message_queue = [] return } @@ -591,7 +591,7 @@ var need_stop = false } } - message_queue.length = 0 + message_queue = [] } var replies = {} diff --git a/internal/shop.cm b/internal/shop.cm index 6f1e5f1e..9d741f37 100644 --- a/internal/shop.cm +++ b/internal/shop.cm @@ -30,7 +30,7 @@ function ensure_dir(path) { if (fd.stat(path).isDirectory) return var parts = array(path, '/') var current = starts_with(path, '/') ? '/' : '' - for (var i = 0; i < parts.length; i++) { + for (var i = 0; i < length(parts); i++) { if (parts[i] == '') continue current += parts[i] + '/' if (!fd.stat(current).isDirectory) { @@ -92,7 +92,7 @@ Shop.get_reports_dir = function() { function get_import_package(name) { var parts = array(name, '/') - if (parts.length > 1) + if (length(parts) > 1) return parts[0] return null @@ -108,16 +108,16 @@ function split_explicit_package_import(path) if (!path) return null var parts = array(path, '/') - if (parts.length < 2) return null + if (length(parts) < 2) return null var looks_explicit = starts_with(path, '/') || (parts[0] && search(parts[0], '.') != null) if (!looks_explicit) return null // Find the longest prefix that is an installed package - for (var i = parts.length - 1; i >= 1; i--) { + for (var i = length(parts) - 1; i >= 1; i--) { var pkg_candidate = text(array(parts, 0, i), '/') var mod_path = text(array(parts, i), '/') - if (!mod_path || mod_path.length == 0) continue + if (!mod_path || length(mod_path) == 0) continue var candidate_dir = get_packages_dir() + '/' + safe_package_path(pkg_candidate) if (fd.is_file(candidate_dir + '/cell.toml')) @@ -159,7 +159,7 @@ function abs_path_to_package(package_dir) } if (starts_with(package_dir, packages_prefix)) - return text(package_dir, packages_prefix.length) + return text(package_dir, length(packages_prefix)) // Check if this local path is the target of a link // If so, return the canonical package name (link origin) instead @@ -206,11 +206,11 @@ Shop.file_info = function(file) { info.package = abs_path_to_package(pkg_dir) if (info.is_actor) - info.name = text(file, pkg_dir.length + 1, file.length - ACTOR_EXT.length) + info.name = text(file, length(pkg_dir) + 1, length(file) - length(ACTOR_EXT)) else if (info.is_module) - info.name = text(file, pkg_dir.length + 1, file.length - MOD_EXT.length) + info.name = text(file, length(pkg_dir) + 1, length(file) - length(MOD_EXT)) else - info.name = text(file, pkg_dir.length + 1) + info.name = text(file, length(pkg_dir) + 1) } return info @@ -219,7 +219,7 @@ Shop.file_info = function(file) { function get_import_name(path) { var parts = array(path, '/') - if (parts.length < 2) return null + if (length(parts) < 2) return null return text(array(parts, 1), '/') } @@ -273,7 +273,7 @@ Shop.load_lock = function() { return {} var content = text(fd.slurp(path)) - if (!content.length) return {} + if (!length(content)) return {} _lock = toml.decode(content) @@ -382,13 +382,13 @@ Shop.get_script_capabilities = function(path) { } function inject_params(inject) { - if (!inject || !inject.length) return '' + if (!inject || !length(inject)) return '' return ', ' + text(inject, ', ') } function inject_values(inject) { var vals = [] - for (var i = 0; i < inject.length; i++) { + for (var i = 0; i < length(inject); i++) { var key = strip_dollar(inject[i]) if (key == 'fd') vals.push(fd) else vals.push(my$_[key]) @@ -1101,7 +1101,7 @@ function install_zip(zip_blob, target_dir) { if (zip.is_directory(i)) continue var filename = zip.get_filename(i) var parts = array(filename, '/') - if (parts.length <= 1) continue + if (length(parts) <= 1) continue parts.shift() var rel_path = text(parts, '/') @@ -1201,7 +1201,7 @@ function get_package_scripts(package) var files = pkg_tools.list_files(package) var scripts = [] - for (var i = 0; i < files.length; i++) { + for (var i = 0; i < length(files); i++) { var file = files[i] if (ends_with(file, '.cm') || ends_with(file, '.ce')) { scripts.push(file) @@ -1300,7 +1300,7 @@ Shop.parse_package = function(locator) { // Extract package name (last component of path) var parts = array(clean, '/') - var name = parts[parts.length - 1] + var name = parts[length(parts) - 1] return { path: clean, diff --git a/internal/testlib.cm b/internal/testlib.cm index 6d6a37ea..abac1d9f 100644 --- a/internal/testlib.cm +++ b/internal/testlib.cm @@ -37,7 +37,7 @@ function ensure_dir(path) { var parts = array(path, '/') var current = starts_with(path, '/') ? '/' : '' - for (var i = 0; i < parts.length; i++) { + for (var i = 0; i < length(parts); i++) { if (parts[i] == '') continue current += parts[i] + '/' if (!fd.is_dir(current)) { diff --git a/link.cm b/link.cm index ab9fa4f8..51daf2f1 100644 --- a/link.cm +++ b/link.cm @@ -34,7 +34,7 @@ function ensure_dir(path) { if (fd.stat(path).isDirectory) return var parts = array(path, '/') var current = starts_with(path, '/') ? '/' : '' - for (var i = 0; i < parts.length; i++) { + for (var i = 0; i < length(parts); i++) { if (parts[i] == '') continue current += parts[i] + '/' if (!fd.stat(current).isDirectory) { diff --git a/package.cm b/package.cm index 1917e8b1..ba91c1ef 100644 --- a/package.cm +++ b/package.cm @@ -123,7 +123,7 @@ package.find_package_dir = function(file) if (fd.is_file(dir)) dir = fd.dirname(dir) - while (dir && dir.length > 0) { + while (dir && length(dir) > 0) { var toml_path = dir + '/cell.toml' if (fd.is_file(toml_path)) { return dir @@ -141,7 +141,7 @@ package.find_package_dir = function(file) // Returns null if no alias is found for the given path package.split_alias = function(name, path) { - if (!path || path.length == 0) { + if (!path || length(path) == 0) { return null } @@ -199,7 +199,7 @@ package.list_files = function(pkg) { var list = fd.readdir(current_dir) if (!list) return - for (var i = 0; i < list.length; i++) { + for (var i = 0; i < length(list); i++) { var item = list[i] if (item == '.' || item == '..') continue if (starts_with(item, '.')) continue @@ -227,7 +227,7 @@ package.list_files = function(pkg) { package.list_modules = function(name) { var files = package.list_files(name) var modules = [] - for (var i = 0; i < files.length; i++) { + for (var i = 0; i < length(files); i++) { if (ends_with(files[i], '.cm')) { modules.push(text(files[i], 0, -3)) } @@ -238,7 +238,7 @@ package.list_modules = function(name) { package.list_programs = function(name) { var files = package.list_files(name) var programs = [] - for (var i = 0; i < files.length; i++) { + for (var i = 0; i < length(files); i++) { if (ends_with(files[i], '.ce')) { programs.push(text(files[i], 0, -3)) } @@ -257,13 +257,13 @@ package.get_flags = function(name, flag_type, target) { // Base flags if (config.compilation && config.compilation[flag_type]) { var base = config.compilation[flag_type] - flags = array(flags, filter(array(base, /\s+/), function(f) { return f.length > 0 })) + flags = array(flags, filter(array(base, /\s+/), function(f) { return length(f) > 0 })) } // Target-specific flags if (target && config.compilation && config.compilation[target] && config.compilation[target][flag_type]) { var target_flags = config.compilation[target][flag_type] - flags = array(flags, filter(array(target_flags, /\s+/), function(f) { return f.length > 0 })) + flags = array(flags, filter(array(target_flags, /\s+/), function(f) { return length(f) > 0 })) } return flags @@ -280,12 +280,12 @@ package.get_c_files = function(name, target, exclude_main) { // Group files by their base name (without target suffix) var groups = {} // base_key -> { generic: file, variants: { target: file } } - for (var i = 0; i < files.length; i++) { + for (var i = 0; i < length(files); i++) { var file = files[i] if (!ends_with(file, '.c') && !ends_with(file, '.cpp')) continue var ext = ends_with(file, '.cpp') ? '.cpp' : '.c' - var base = text(file, 0, -ext.length) + var base = text(file, 0, -length(ext)) var name_part = fd.basename(base) var dir_part = fd.dirname(base) var dir = (dir_part && dir_part != '.') ? dir_part + '/' : '' @@ -295,12 +295,12 @@ package.get_c_files = function(name, target, exclude_main) { var variant_target = null var generic_name = name_part - for (var t = 0; t < known_targets.length; t++) { + for (var t = 0; t < length(known_targets); t++) { var suffix = '_' + known_targets[t] if (ends_with(name_part, suffix)) { is_variant = true variant_target = known_targets[t] - generic_name = text(name_part, 0, -suffix.length) + generic_name = text(name_part, 0, -length(suffix)) break } } diff --git a/pronto.cm b/pronto.cm index 9d491cc1..45160e1e 100644 --- a/pronto.cm +++ b/pronto.cm @@ -10,7 +10,7 @@ function make_reason(factory, excuse, evidence) { } function is_requestor(fn) { - return is_function(fn) && (fn.length == 1 || fn.length == 2) + return is_function(fn) && (length(fn) == 1 || length(fn) == 2) } function check_requestors(list, factory) { @@ -19,7 +19,7 @@ function check_requestors(list, factory) { } function check_callback(cb, factory) { - if (!is_function(cb) || cb.length != 2) + if (!is_function(cb) || length(cb) != 2) throw make_reason(factory, 'Not a callback.', cb) } @@ -27,7 +27,7 @@ function check_callback(cb, factory) { // Tries each requestor in order until one succeeds. function fallback(requestor_array) { def factory = 'fallback' - if (!is_array(requestor_array) || requestor_array.length == 0) + if (!is_array(requestor_array) || length(requestor_array) == 0) throw make_reason(factory, 'Empty requestor array.') check_requestors(requestor_array, factory) @@ -47,7 +47,7 @@ function fallback(requestor_array) { function try_next() { if (cancelled) return - if (index >= requestor_array.length) { + if (index >= length(requestor_array)) { callback(null, make_reason(factory, 'All requestors failed.')) return } @@ -83,7 +83,7 @@ function parallel(requestor_array, throttle, need) { throw make_reason(factory, 'Not an array.', requestor_array) check_requestors(requestor_array, factory) - def length = requestor_array.length + def length = length(requestor_array) if (length == 0) return function(callback, value) { callback([]) } @@ -165,11 +165,11 @@ function parallel(requestor_array, throttle, need) { // Runs requestors in parallel, returns first success(es). function race(requestor_array, throttle, need) { def factory = 'race' - if (!is_array(requestor_array) || requestor_array.length == 0) + if (!is_array(requestor_array) || length(requestor_array) == 0) throw make_reason(factory, 'Empty requestor array.') check_requestors(requestor_array, factory) - def length = requestor_array.length + def length = length(requestor_array) if (need == null) need = 1 if (!is_number(need) || need < 1 || need > length) throw make_reason(factory, 'Bad need.', need) @@ -254,7 +254,7 @@ function sequence(requestor_array) { throw make_reason(factory, 'Not an array.', requestor_array) check_requestors(requestor_array, factory) - if (requestor_array.length == 0) + if (length(requestor_array) == 0) return function(callback, value) { callback(value) } return function sequence_requestor(callback, value) { @@ -273,7 +273,7 @@ function sequence(requestor_array) { function run_next(val) { if (cancelled) return - if (index >= requestor_array.length) { + if (index >= length(requestor_array)) { callback(val) return } diff --git a/source/quickjs.c b/source/quickjs.c index 1c6c6453..25e5ee83 100644 --- a/source/quickjs.c +++ b/source/quickjs.c @@ -36282,15 +36282,6 @@ static JSValue js_blob_pad_q(JSContext *ctx, JSValueConst this_val, return JS_NewBool(ctx, blob_pad_check(bd, from, block_size)); } -/* blob.length getter */ -static JSValue js_blob_get_length(JSContext *ctx, JSValueConst this_val) -{ - blob *bd = js_get_blob(ctx, this_val); - if (!bd) - return JS_ThrowTypeError(ctx, "length: not called on a blob"); - return JS_NewInt64(ctx, bd->length); -} - static const JSCFunctionListEntry js_blob_proto_funcs[] = { /* Write methods */ JS_CFUNC_DEF("write_bit", 1, js_blob_write_bit), @@ -36310,9 +36301,6 @@ static const JSCFunctionListEntry js_blob_proto_funcs[] = { JS_CFUNC_DEF("read_fit", 2, js_blob_read_fit), JS_CFUNC_DEF("read_text", 1, js_blob_read_text), JS_CFUNC_DEF("pad?", 2, js_blob_pad_q), - - /* Length property getter */ - JS_CGETSET_DEF("length", js_blob_get_length, NULL), }; /* ============================================================================ diff --git a/tests/blob.cm b/tests/blob.cm index 9bbd513b..ccd80b37 100644 --- a/tests/blob.cm +++ b/tests/blob.cm @@ -17,12 +17,12 @@ function assertEqual(actual, expected, message) { return { test_create_empty_blob: function() { var b = Blob(); - assertEqual(b.length, 0, "Empty blob should have length 0"); + assertEqual(length(b), 0, "Empty blob should have length 0"); }, test_create_blob_with_capacity: function() { var b = Blob(100); - assertEqual(b.length, 0, "New blob with capacity should still have length 0"); + assertEqual(length(b), 0, "New blob with capacity should still have length 0"); }, test_write_and_read_single_bit: function() { @@ -31,7 +31,7 @@ return { b.write_bit(false); b.write_bit(1); b.write_bit(0); - assertEqual(b.length, 4, "Should have 4 bits after writing"); + assertEqual(length(b), 4, "Should have 4 bits after writing"); stone(b); assertEqual(b.read_logical(0), true, "First bit should be true"); @@ -95,7 +95,7 @@ return { var b2 = Blob(10); b2.write_blob(b1); b2.write_bit(false); - assertEqual(b2.length, 4, "Combined blob should have 4 bits"); + assertEqual(length(b2), 4, "Combined blob should have 4 bits"); stone(b2); assertEqual(b2.read_logical(0), true); @@ -114,7 +114,7 @@ return { var b2 = Blob(b1); stone(b2); - assertEqual(b2.length, 4, "Copied blob should have same length"); + assertEqual(length(b2), 4, "Copied blob should have same length"); assertEqual(b2.read_logical(0), true); assertEqual(b2.read_logical(3), true); }, @@ -128,7 +128,7 @@ return { var b2 = Blob(b1, 2, 7); stone(b2); - assertEqual(b2.length, 5, "Partial copy should have 5 bits"); + assertEqual(length(b2), 5, "Partial copy should have 5 bits"); assertEqual(b2.read_logical(0), true); assertEqual(b2.read_logical(2), true); }, @@ -167,7 +167,7 @@ return { b.write_bit(true); b.write_pad(8); - assertEqual(b.length, 8, "Should be padded to 8 bits"); + assertEqual(length(b), 8, "Should be padded to 8 bits"); stone(b); assert(b['pad?'](3, 8), "Should detect valid padding at position 3"); @@ -183,7 +183,7 @@ return { var b2 = b1.read_blob(4, 12); stone(b2); - assertEqual(b2.length, 8, "Read blob should have 8 bits"); + assertEqual(length(b2), 8, "Read blob should have 8 bits"); assertEqual(b2.read_logical(2), true); assertEqual(b2.read_logical(5), true); @@ -248,20 +248,20 @@ return { b.write_bit(false); b.write_number(-999.999); - var originalLength = b.length; + var originalLength = length(b); stone(b); var b2 = Blob(b); stone(b2); - assertEqual(b2.length, originalLength, "Copy should have same length"); + assertEqual(length(b2), originalLength, "Copy should have same length"); assertEqual(b2.read_text(0), "Test", "First text should match"); }, test_zero_capacity_blob: function() { var b = Blob(0); - assertEqual(b.length, 0, "Zero capacity blob should have length 0"); + assertEqual(length(b), 0, "Zero capacity blob should have length 0"); b.write_bit(true); - assertEqual(b.length, 1, "Should expand when writing"); + assertEqual(length(b), 1, "Should expand when writing"); }, test_large_blob_handling: function() { @@ -272,7 +272,7 @@ return { b.write_bit(i % 7 == 0); } - assertEqual(b.length, testSize, "Should have " + testSize + " bits"); + assertEqual(length(b), testSize, "Should have " + testSize + " bits"); stone(b); assertEqual(b.read_logical(0), true, "Bit 0 should be true"); diff --git a/tests/chunkread.cm b/tests/chunkread.cm index 83d7062c..616ecedb 100644 --- a/tests/chunkread.cm +++ b/tests/chunkread.cm @@ -20,11 +20,7 @@ return { while(true) { var chunk = fd.read(f2, chunksize); data.write_blob(chunk); - // chunk.length is in bits, chunksize is bytes? - // fd.read usually takes bytes. Blob.length is bits. - // If chunk is blob, length is bits. - // fd.read returns blob. - if (chunk.length < chunksize * 8) break; + if (length(chunk) < chunksize * 8) break; } fd.close(f2) log.console(`read took ${time.number()-st}`) diff --git a/tests/guid.cm b/tests/guid.cm index 757c8c53..3833204a 100644 --- a/tests/guid.cm +++ b/tests/guid.cm @@ -12,6 +12,6 @@ return { st = time.number()-st log.console(`took ${btime*1000000} us to make blob; took ${st*1000000} us to make it text`) log.console(lower(guid)) - log.console(guid.length) + log.console(length(guid)) } } diff --git a/tests/http.cm b/tests/http.cm index 77491fc2..85289397 100644 --- a/tests/http.cm +++ b/tests/http.cm @@ -3,5 +3,5 @@ var http = use('http') return function() { var url = "http://example.com" var b2 = http.fetch(url) - if (b2.length == 0) throw "Empty response" + if (length(b2) == 0) throw "Empty response" } \ No newline at end of file diff --git a/tests/miniz.cm b/tests/miniz.cm index f7c9c670..84009cdd 100644 --- a/tests/miniz.cm +++ b/tests/miniz.cm @@ -54,9 +54,9 @@ return { var reader = miniz.read(zip_blob) var listed = reader.list() - if (listed.length != reader.count()) + if (length(listed) != reader.count()) throw "list/count mismatch" - if (listed.length != 2) + if (length(listed) != 2) throw "unexpected entry count" } finally { try { fd.unlink(ZIP_PATH) } catch(e) {} diff --git a/tests/nota.cm b/tests/nota.cm index 4d92e92d..bb03a0fa 100644 --- a/tests/nota.cm +++ b/tests/nota.cm @@ -8,7 +8,7 @@ function stone_if_needed(b) { if (!stone.p(b)) stone(b) } function bytes_to_blob(bytes) { var b = blob() - for (var i = 0; i < bytes.length; i++) { + for (var i = 0; i < length(bytes); i++) { var byte = bytes[i] for (var bit = 7; bit >= 0; bit--) b.write_bit((byte >> bit) & 1) } @@ -40,7 +40,7 @@ function deepCompare(expected, actual, path) { if (is_blob(expected) && is_blob(actual)) { stone_if_needed(expected); stone_if_needed(actual) if (expected.length != actual.length) - return { passed: false, messages: [`blob length mismatch at ${path}: ${expected.length} vs ${actual.length}`] } + return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] } for (var i = 0; i < expected.length; i++) { if (expected.read_logical(i) != actual.read_logical(i)) return { passed: false, messages: [`blob bit mismatch at ${path}[${i}]`] } @@ -52,7 +52,7 @@ function deepCompare(expected, actual, path) { if (expected.length != actual.length) return { passed: false, - messages: [`Array length mismatch at ${path}: expected ${expected.length}, got ${actual.length}`] + messages: [`Array length mismatch at ${path}: expected ${length(expected)}, got ${length(actual)}`] }; var messages = []; for (var i = 0; i < expected.length; i++) { @@ -174,7 +174,7 @@ var testCases = [ ]; var tests = {}; -for (var i = 0; i < testCases.length; i++) { +for (var i = 0; i < length(testCases); i++) { var t = testCases[i]; tests[t.name] = makeTest(t); } diff --git a/tests/suite.cm b/tests/suite.cm index 527d42ea..05ae04db 100644 --- a/tests/suite.cm +++ b/tests/suite.cm @@ -1905,7 +1905,7 @@ return { var fn = function(a, b) { return a + b } var caught = false try { - var x = fn.length + var x = length(fn) } catch (e) { caught = true } @@ -3543,7 +3543,7 @@ return { test_text_split_text: function() { var text = "hello world" var result = array(text, " ") - if (result.length != 2) throw "text split failed" + if (length(result) != 2) throw "text split failed" if (result[0] != "hello") throw "text split failed" if (result[1] != "world") throw "text split failed" }, @@ -3551,7 +3551,7 @@ return { test_text_split_regex: function() { var text = "hello world" var result = array(text, /\s+/) - if (result.length != 2) throw "text split failed" + if (length(result) != 2) throw "text split failed" if (result[0] != "hello") throw "text split failed" if (result[1] != "world") throw "text split failed" }, diff --git a/tests/toml.cm b/tests/toml.cm index a40b1001..63a8976d 100644 --- a/tests/toml.cm +++ b/tests/toml.cm @@ -8,9 +8,9 @@ function deep_equal(a, b) { if (is_object(a)) { var keys_a = array(a) var keys_b = array(b) - if (keys_a.length != keys_b.length) return false + if (length(keys_a) != length(keys_b)) return false - for (var i = 0; i < keys_a.length; i++) { + for (var i = 0; i < length(keys_a); i++) { if (!deep_equal(a[keys_a[i]], b[keys_a[i]])) return false } return true diff --git a/tests/wota.cm b/tests/wota.cm index 76f2fae6..8a004f70 100644 --- a/tests/wota.cm +++ b/tests/wota.cm @@ -21,9 +21,9 @@ function deep_compare(expected, actual, path) { if (is_blob(expected) && is_blob(actual)) { stone_if_needed(expected); stone_if_needed(actual) - if (expected.length != actual.length) - return { passed: false, messages: [`blob length mismatch at ${path}: ${expected.length} vs ${actual.length}`] } - for (var i = 0; i < expected.length; i++) { + if (length(expected) != length(actual)) + return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${actual)}`] } + for (var i = 0; i < length(expected); i++) { if (expected.read_logical(i) != actual.read_logical(i)) return { passed: false, messages: [`blob bit mismatch at ${path}[${i}]`] } } @@ -31,16 +31,16 @@ function deep_compare(expected, actual, path) { } if (is_array(expected) && is_array(actual)) { - if (expected.length != actual.length) - return { passed: false, messages: [`Array length mismatch at ${path}: ${expected.length} vs ${actual.length}`] } + if (length(expected) != length(actual)) + return { passed: false, messages: [`Array length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] } var msgs = [] - for (var i = 0; i < expected.length; i++) { + for (var i = 0; i < length(expected); i++) { var res = deep_compare(expected[i], actual[i], `${path}[${i}]`) if (!res.passed) { for(var m of res.messages) msgs.push(m) } } - return { passed: msgs.length == 0, messages: msgs } + return { passed: length(msgs) == 0, messages: msgs } } if (is_object(expected) && is_object(actual)) { @@ -55,7 +55,7 @@ function deep_compare(expected, actual, path) { for(var m of res.messages) msgs.push(m) } } - return { passed: msgs.length == 0, messages: msgs } + return { passed: length(msgs) == 0, messages: msgs } } return { passed: false, messages: [`Value mismatch at ${path}: ${JSON.stringify(expected)} vs ${JSON.stringify(actual)}`] } @@ -120,7 +120,7 @@ function make_test(t) { } var tests = {} -for (var i = 0; i < testCases.length; i++) { +for (var i = 0; i < length(testCases); i++) { var t = testCases[i] var name = t.name || ('case_' + i) tests[name] = make_test(t) diff --git a/toml.cm b/toml.cm index 559350ad..1eb41b8b 100644 --- a/toml.cm +++ b/toml.cm @@ -31,7 +31,7 @@ function parse_toml(toml_text) { var current_section = result var current_section_name = '' - for (var i = 0; i < lines.length; i++) { + for (var i = 0; i < length(lines); i++) { var line = trim(lines[i]) if (line == null) line = lines[i] // Skip empty lines and comments @@ -46,7 +46,7 @@ function parse_toml(toml_text) { current_section = result current_section_name = text(section_path, '.') - for (var j = 0; j < section_path.length; j++) { + for (var j = 0; j < length(section_path); j++) { var key = section_path[j] // Only treat null as "missing"; do not clobber false/0/"" @@ -112,7 +112,7 @@ function parse_key_path(str) { var current = '' var in_quote = false - for (var i = 0; i < str.length; i++) { + for (var i = 0; i < length(str); i++) { var c = str[i] if (c == '"' && (i == 0 || str[i - 1] != '\\')) { in_quote = !in_quote @@ -128,7 +128,7 @@ function parse_key_path(str) { var tail = trim(current) if (tail == null) tail = trim(current) - if (tail.length > 0) parts.push(parse_key(tail)) + if (length(tail) > 0) parts.push(parse_key(tail)) return parts } @@ -145,7 +145,7 @@ function parse_array(str) { var current = '' var in_quotes = false - for (var i = 0; i < str.length; i++) { + for (var i = 0; i < length(str); i++) { var ch = str[i] if (ch == '"' && (i == 0 || str[i - 1] != '\\')) { @@ -191,7 +191,7 @@ function encode_toml(obj) { if (is_number(value)) return text(value) if (is_array(value)) { var items = [] - for (var i = 0; i < value.length; i++) items.push(encode_value(value[i])) + for (var i = 0; i < length(value); i++) items.push(encode_value(value[i])) return '[' + text(items, ', ') + ']' } return text(value) @@ -206,7 +206,7 @@ function encode_toml(obj) { // First pass: encode top-level simple values var keys = array(obj) - for (var i = 0; i < keys.length; i++) { + for (var i = 0; i < length(keys); i++) { var key = keys[i] var value = obj[key] if (!is_object(value)) result.push(quote_key(key) + ' = ' + encode_value(value)) @@ -215,7 +215,7 @@ function encode_toml(obj) { // Second pass: encode nested objects function encode_section(o, path) { var keys = array(o) - for (var i = 0; i < keys.length; i++) { + for (var i = 0; i < length(keys); i++) { var key = keys[i] var value = o[key] @@ -226,7 +226,7 @@ function encode_toml(obj) { // Direct properties var section_keys = array(value) - for (var j = 0; j < section_keys.length; j++) { + for (var j = 0; j < section_length(keys); j++) { var sk = section_keys[j] var sv = value[sk] if (!is_object(sv)) result.push(quote_key(sk) + ' = ' + encode_value(sv))