From 86530871e6a43cd7ceb6aa8bd04d1b33313cbaca Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 19 Jan 2026 01:06:51 -0600 Subject: [PATCH] rm of --- action.cm | 2 +- camera.cm | 2 +- clay.cm | 9 ++--- clay_input.cm | 8 ++-- color.cm | 41 +++++++++++---------- controller.cm | 24 ++++++------ core.cm | 10 ++--- emacs.cm | 4 +- examples/bunnymark/main.ce | 3 +- examples/chess/grid.cm | 6 +-- fx_graph.cm | 10 ++--- gestures.cm | 4 +- graphics.cm | 4 +- playdate.cm | 15 ++++---- resources.cm | 43 ++++++++++++---------- sdl_gpu.cm | 75 ++++++++++++++++++-------------------- tests/animation.ce | 2 +- tests/camera.ce | 3 +- tween.cm | 8 ++-- 19 files changed, 133 insertions(+), 140 deletions(-) diff --git a/action.cm b/action.cm index ae3598b1..4ffc3de5 100644 --- a/action.cm +++ b/action.cm @@ -238,7 +238,7 @@ action.on_input = function(action_id, evt) // Send all matched actions (only if we found mappings - this means it's a raw input) if (length(matched_actions) > 0) { for (var i = 0; i < length(matched_actions); i++) { -// scene.recurse(game.root, 'on_input', matched_actions[i], evt) +// scene.recurse(game.root, 'on_input', [matched_actions[i], evt]) } } } diff --git a/camera.cm b/camera.cm index 50df2731..633ce583 100644 --- a/camera.cm +++ b/camera.cm @@ -5,7 +5,7 @@ var cam = {} letterbox overscan stretch - ... or simply 'null' for no presentation + .. or simply 'null' for no presentation */ function rect_contains_pt(rect, x, y) { diff --git a/clay.cm b/clay.cm index f28eb0da..615e9ef2 100644 --- a/clay.cm +++ b/clay.cm @@ -202,12 +202,9 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s } // Children - for (var child of node.children) { - var child_drawables = build_drawables(child, root_height, vis_x, vis_y, current_scissor, parent_layer + 0.01) - for (var i = 0; i < length(child_drawables); i++) { - drawables.push(child_drawables[i]) - } - } + arrfor(node.children, function(child) { + drawables = array(drawables, build_drawables(child, root_height, vis_x, vis_y, current_scissor, parent_layer + 0.01)) + }) return drawables } diff --git a/clay_input.cm b/clay_input.cm index f5f2f872..67d9d4d4 100644 --- a/clay_input.cm +++ b/clay_input.cm @@ -76,7 +76,7 @@ clay_input.get_actionable = function get_actionable(tree_root) { function walk(node) { if (node.config.action) actionable.push(node) if (node[clay.CHILDREN]) - for (var child of node[clay.CHILDREN]) walk(child) + arrfor(node[clay.CHILDREN], walk) } walk(tree_root) return actionable @@ -87,7 +87,7 @@ clay_input.filter = function filter(tree_root, predicate) { function rec(node) { if (predicate(node)) results.push(node) if (node[clay.CHILDREN]) - for (var child of node[clay.CHILDREN]) rec(child) + arrfor(node[clay.CHILDREN], rec) } rec(tree_root) return results @@ -97,10 +97,10 @@ clay_input.find_by_id = function find_by_id(tree_root, id) { function rec(node) { if (node.id == id) return node if (node[clay.CHILDREN]) - for (var child of node[clay.CHILDREN]) { + arrfor(node[clay.CHILDREN], function(child) { var f = rec(child) if (f) return f - } + }) return null } return rec(tree_root) diff --git a/color.cm b/color.cm index 56aedd9f..62f480a4 100644 --- a/color.cm +++ b/color.cm @@ -93,11 +93,11 @@ Color.normalize = function (c) { return n; }; - for (var p of array(c)) { - if (!is_object(c[p])) continue; + arrfor(array(c), function(p) { + if (!is_object(c[p])) return; if (!is_array(c[p])) { Color.normalize(c[p]); - continue; + return; } // Add alpha channel if not present @@ -107,12 +107,12 @@ Color.normalize = function (c) { // Check if any values are > 1 (meaning they're in 0-255 format) var needs_conversion = false; - for (var color of c[p]) { + arrfor(c[p], function(color) { if (color > 1) { needs_conversion = true; - break; + return; } - } + }) // Convert from 0-255 to 0-1 if needed if (needs_conversion) { @@ -120,7 +120,7 @@ Color.normalize = function (c) { } c[p].alpha = add_a; - } + }) }; Color.normalize(Color); @@ -181,22 +181,25 @@ ColorMap.Viridis = ColorMap.makemap({ Color.normalize(ColorMap); -ColorMap.sample = function (t, map = this) { - if (t < 0) return map[0]; - if (t > 1) return map[1]; +ColorMap.sample = function(t, map = this) { + if (t < 0) return map[0] + if (t > 1) return map[1] - var lastkey = 0; - for (var key of sorted(array(map))) { + var keys = sorted(array(map)) + var lastkey = 0 + + for (var i = 0; i < length(keys); i++) { + var key = keys[i] if (t < key) { - var b = map[key]; - var a = map[lastkey]; - var tt = (key - lastkey) * t; - return a.lerp(b, tt); + var b = map[key] + var a = map[lastkey] + var tt = (t - lastkey) / (key - lastkey) + return a.lerp(b, tt) } - lastkey = key; + lastkey = key } - return map[1]; -}; + return map[1] +} ColorMap.doc = { sample: "Sample a given colormap at the given percentage (0 to 1).", diff --git a/controller.cm b/controller.cm index b9f7431f..ff927b9c 100644 --- a/controller.cm +++ b/controller.cm @@ -211,27 +211,27 @@ var Player = { }, mouse_input(type, ...args) { - for (var pawn of reverse([...this.pawns])) { - if (is_function(pawn.inputs?.mouse?.[type])) { + arrfor(array(this.pawns), function(pawn) { + if (is_function(pawn.inputs.mouse[type])) { call(pawn.inputs.mouse[type], pawn, ...args); pawn.inputs.post?.call(pawn); if (!pawn.inputs.fallthru) return; } - } + }, true) }, char_input(c) { - for (var pawn of reverse([...this.pawns])) { - if (is_function(pawn.inputs?.char)) { + arrfor(array(this.pawns), function(pawn) { + if (is_function(pawn.inputs.char)) { call(pawn.inputs.char, pawn, c); pawn.inputs.post?.call(pawn); if (!pawn.inputs.fallthru) return; } - } + }, true) }, joy_input(name, joystick) { - for (var pawn of reverse([...this.pawns])) { + arrfor(array(this.pawns), function(pawn) { if (!pawn.inputs) return; if (!pawn.inputs.joystick) return; if (!pawn.inputs.joystick[name]) return; @@ -244,11 +244,11 @@ var Player = { if (input.keyboard.down(joystick.dy)) y--; pawn.inputs.joystick[name](x, y); - } + }, true) }, raw_input(cmd, state, ...args) { - for (var pawn of reverse([...this.pawns])) { + arrfor(array(this.pawns), function(pawn) { var inputs = pawn.inputs; if (!inputs[cmd]) { @@ -281,19 +281,19 @@ var Player = { if (state == "released") call(inputs.release_post, pawn); if (inputs.block) return; if (consumed) return; - } + }, true) }, obj_controlled(obj) { for (var p in Player.players) { - if (p.pawns.has(obj)) return true; + if (p.pawns[obj]) return true; } return false; }, print_pawns() { - arrfor(reverse([...this.pawns]), x => log.console(x)) + arrfor(array(this.pawns), x => log.console(x), true) }, create() { diff --git a/core.cm b/core.cm index e5e0e67b..5c99ea47 100644 --- a/core.cm +++ b/core.cm @@ -100,7 +100,7 @@ function _main_loop() { // Get input module for auto-ingestion var input_mod = use('input') - for (var ev of evts) { + arrfor(evts, function(ev) { if (_config.imgui || _config.editor) { imgui.process_event(ev) } @@ -127,7 +127,7 @@ function _main_loop() { if (_config.input) { _config.input(ev_obj) } - } + }) // Update if (_config.update) { @@ -198,11 +198,7 @@ function _main_loop() { if (length(_fps_samples) > _fps_sample_count) { _fps_samples.shift() } - var avg_frame_time = 0 - for (var i = 0; i < length(_fps_samples); i++) { - avg_frame_time += _fps_samples[i] - } - avg_frame_time = avg_frame_time / length(_fps_samples) + var avg_frame_time = reduce(_fps_samples, function(a,b) { return a+b }) / length(_fps_samples) _current_fps = avg_frame_time > 0 ? 1 / avg_frame_time : 0 // Schedule next frame diff --git a/emacs.cm b/emacs.cm index ff8cd96f..e613268d 100644 --- a/emacs.cm +++ b/emacs.cm @@ -86,9 +86,9 @@ action.on_input = function(action_id, action_data) if (this.prefix_key) { var full_command = this.prefix_key + " " + emacs_notation this.prefix_key = null // Reset prefix key -// scene.recurse(game.root, 'on_input', full_command, action_data) +// scene.recurse(game.root, 'on_input', [full_command, action_data]) } else { -// scene.recurse(game.root, 'on_input', emacs_notation, action_data) +// scene.recurse(game.root, 'on_input', [emacs_notation, action_data]) } } diff --git a/examples/bunnymark/main.ce b/examples/bunnymark/main.ce index ab18e48c..1772b2f7 100644 --- a/examples/bunnymark/main.ce +++ b/examples/bunnymark/main.ce @@ -31,8 +31,7 @@ this.update = function(dt) { var currentFPS = 1 / dt fpsSamples.push(currentFPS) if (length(fpsSamples) > 60) fpsSamples.shift() - var sum = 0 - for (var f of fpsSamples) sum += f + var sum = reduce(fpsSamples, function(a,b) { return a + b }) fpsAvg = sum / length(fpsSamples) // If left mouse is down, spawn some more bunnies: diff --git a/examples/chess/grid.cm b/examples/chess/grid.cm index db845c21..f8859322 100644 --- a/examples/chess/grid.cm +++ b/examples/chess/grid.cm @@ -47,10 +47,10 @@ grid.prototype = { each(fn) { for (var y = 0; y < this.height; y++) { for (var x = 0; x < this.width; x++) { - def list = this.cells[y][x]; - for (var entity of list) { + def list = this.cells[y][x] + arrfor(list, function(entity) { fn(entity, entity.coord); - } + }) } } }, diff --git a/fx_graph.cm b/fx_graph.cm index f90731e6..fc0cff78 100644 --- a/fx_graph.cm +++ b/fx_graph.cm @@ -81,11 +81,11 @@ fx_graph.execute = function(backend) { var node_outputs = {} var all_commands = [] - for (var node of sorted) { + arrfor(sorted, function(node) { var executor = NODE_EXECUTORS[node.type] if (!executor) { log.console(`fx_graph: No executor for node type: ${node.type}`) - continue + return } var resolved_params = this.resolve_inputs(node.params, node_outputs) @@ -96,11 +96,9 @@ fx_graph.execute = function(backend) { // Collect commands from this node if (result && result.commands) { - for (var cmd of result.commands) { - all_commands.push(cmd) - } + all_commands = array(all_commands, result.commands) } - } + }) return {commands: all_commands} } diff --git a/gestures.cm b/gestures.cm index b683f45a..4e877111 100644 --- a/gestures.cm +++ b/gestures.cm @@ -61,7 +61,7 @@ gesture.on_input = function(action_id, action) { if (abs(d) >= this.PINCH_TH) { var gesture_type = d > 0 ? 'pinch_out' : 'pinch_in' -// scene.recurse(game.root, 'on_input', gesture_type, { delta: d }) +// scene.recurse(game.root, 'on_input', [gesture_type, { delta: d }]) this.startDist = currentDist } } @@ -85,7 +85,7 @@ gesture.on_input = function(action_id, action) { ? (dx > 0 ? 'swipe_right' : 'swipe_left') : (dy > 0 ? 'swipe_down' : 'swipe_up') audio.play('swipe') -// scene.recurse(game.root, 'on_input', dir, {pressed:true}) +// scene.recurse(game.root, 'on_input', [dir, {pressed:true}]) } } } diff --git a/graphics.cm b/graphics.cm index 916a4cd4..6b2cbb2a 100644 --- a/graphics.cm +++ b/graphics.cm @@ -178,12 +178,12 @@ function create_image(path){ return makeAnim(wrapFrames(raw.frames), !!raw.loop); def anims = {}; - for(def [name, anim] of Object.entries(raw)){ + arrfor(Object.entries(raw), function([name, anim]) { if(anim && is_array(anim.frames)) anims[name] = makeAnim(wrapFrames(anim.frames), !!anim.loop); else if(anim && anim.surface) anims[name] = new graphics.Image(anim.surface); - } + }) if(length(array(anims))) return anims; } diff --git a/playdate.cm b/playdate.cm index 5e90a2a9..0e53bec9 100644 --- a/playdate.cm +++ b/playdate.cm @@ -34,7 +34,7 @@ PlaydateBackend.prototype.get_or_create_target = function(width, height, key) { this.target_pool[pool_key] = [] // Reuse from pool - for (var target of this.target_pool[pool_key]) { + arrfor(this.target_pool[pool_key], function(target) { if (!target.in_use) { target.in_use = true // Clear bitmap for reuse @@ -43,7 +43,7 @@ PlaydateBackend.prototype.get_or_create_target = function(width, height, key) { this.pd.graphics.popContext() return target } - } + }) // Create new LCDBitmap var bitmap = this.pd.graphics.newBitmap(width, height, this.pd.graphics.kColorClear) @@ -61,10 +61,11 @@ PlaydateBackend.prototype.get_or_create_target = function(width, height, key) { } PlaydateBackend.prototype.release_all_targets = function() { - for (var pool_key in this.target_pool) { - for (var target of this.target_pool[pool_key]) + arrfor(array(this.target_pool), function(key) { + arrfor(this.target_pool[key], function(target) { target.in_use = false - } + }) + }) } // ======================================================================== @@ -73,9 +74,9 @@ PlaydateBackend.prototype.release_all_targets = function() { PlaydateBackend.prototype.execute = function(commands) { // No command buffer concept - execute immediately - for (var cmd of commands) { + arrfor(commands, function(cmd) { this.execute_command(cmd) - } + }) this.release_all_targets() } diff --git a/resources.cm b/resources.cm index 63a8cb5d..4ba07397 100644 --- a/resources.cm +++ b/resources.cm @@ -2,9 +2,9 @@ var io = use('cellfs') function hashify(fn) { var hash = {} - return function(...args) { - var key = args[0] - if (hash[key] == null) hash[key] = fn(...args) + return function(arg) { + var key = arg + if (hash[key] == null) hash[key] = fn(arg) return hash[key] } } @@ -52,10 +52,15 @@ function find_in_path(filename, exts = []) { // Only check extensions if exts is provided and not empty if (length(exts) > 0) { - for (var ext of exts) { + var cand = null + arrfor(exts, function(ext) { var candidate = filename + '.' + ext - if (io.exists(candidate) && !io.is_directory(candidate)) return candidate - } + if (io.exists(candidate) && !io.is_directory(candidate)){ + cand = candidate + return true + } + }, null, true) + if (cand != null) return cand } else { // Fallback to extensionless file only if no extensions are specified var candidate = filename @@ -92,11 +97,11 @@ function read_ignore(dir) { var patterns = [] if (io.exists(path)) { var lines = array(io.slurp(path), '\n') - for (var line of lines) { + arrfor(lines, function(line) { line = trim(line) - if (!line || starts_with(line, '#')) continue + if (!line || starts_with(line, '#')) return patterns.push(line) - } + }) } return patterns } @@ -107,17 +112,17 @@ Resources.getAllFiles = function(dir = "") { var patterns = read_ignore(dir) var all = io.globfs(patterns, dir) var results = [] - for (var f of all) { + arrfor(all, function(f) { var fullPath = dir + '/' + f try { var st = io.stat(fullPath) // skip directories (filesize=0) or unrecognized extension - if (!st.filesize) continue + if (!st.filesize) return var ext = getExtension(f) - if (!isRecognizedExtension(ext)) continue + if (!isRecognizedExtension(ext)) return results.push(fullPath) } catch(e) {} - } + }) return results } @@ -126,26 +131,26 @@ Resources.gatherStats = function(filePaths) { var stats = { scripts:0, images:0, sounds:0, fonts:0, lib:0, other:0, total:length(filePaths) } - for (var path of filePaths) { + arrfor(filePaths, function(path) { var ext = getExtension(path) if (find(Resources.scripts, ext) != null) { stats.scripts++ - continue + return } if (find(Resources.images, ext) != null) { stats.images++ - continue + return } if (find(Resources.sounds, ext) != null) { stats.sounds++ - continue + return } if (find(Resources.fonts, ext) != null) { stats.fonts++ - continue + return } stats.other++ - } + }) return stats } diff --git a/sdl_gpu.cm b/sdl_gpu.cm index b6a2c46c..2ae915c7 100644 --- a/sdl_gpu.cm +++ b/sdl_gpu.cm @@ -782,21 +782,21 @@ sdl_gpu.get_or_create_target = function(width, height, key) { // Reuse from pool if available // 1. Check if a target with this exact key already exists if (key) { - for (var target of _target_pool[pool_key]) { - if (target.key == key) { - target.in_use = true - return target - } + var pool = _target_pool[pool_key] + var idx = find(pool, function(t) { return t.key == key }) + if (idx != null) { + pool[idx].in_use = true + return pool[idx] } } // 2. Otherwise prefer most recently used (LIFO) or just first available - for (var target of _target_pool[pool_key]) { - if (!target.in_use) { - target.in_use = true - target.key = key - return target - } + var pool = _target_pool[pool_key] + var idx = find(pool, function(t) { return !t.in_use }) + if (idx != null) { + pool[idx].in_use = true + pool[idx].key = key + return pool[idx] } // Create new render target texture @@ -827,10 +827,11 @@ sdl_gpu.get_or_create_target = function(width, height, key) { } sdl_gpu.release_all_targets = function() { - for (var pool_key in _target_pool) { - for (var target of _target_pool[pool_key]) + arrfor(array(_target_pool), function(pool_key) { + arrfor(_target_pool[pool_key], function(target) { target.in_use = false - } + }) + }) } // ======================================================================== @@ -1118,13 +1119,12 @@ sdl_gpu.execute_commands = function(commands, window_size, dbg = false) { function _preload_textures(commands) { var paths = {} - for (var cmd of commands) { + arrfor(commands, function(cmd) { if (cmd.cmd == 'draw_batch' && cmd.texture) { - if (is_text(cmd.texture)) { + if (is_text(cmd.texture)) paths[cmd.texture] = true - } } - } + }) // Load all textures for (var path in paths) { @@ -1148,7 +1148,7 @@ function _execute_commands(commands, window_size) { return _swapchain_tex } - for (var cmd of commands) { + arrfor(commands, function(cmd) { switch (cmd.cmd) { case 'begin_render': // Flush pending draws @@ -1352,7 +1352,7 @@ function _execute_commands(commands, window_size) { // Submit command buffer break } - } + }) // Final flush if (current_pass && length(pending_draws) > 0) { @@ -1370,7 +1370,7 @@ function _flush_draws(cmd_buffer, pass, draws, camera, target) { var current_batch = null // Iterate draws preserving order - for (var draw of draws) { + arrfor(draws, function(draw) { if (draw.cmd == 'draw_batch') { // Sprite batch handling var tex_path = draw.texture || '_white' @@ -1386,9 +1386,7 @@ function _flush_draws(cmd_buffer, pass, draws, camera, target) { // Append sprites if (draw.geometry && draw.geometry.sprites) { - for (var s of draw.geometry.sprites) { - current_batch.sprites.push(s) - } + current_batch.sprites = array(current_batch.sprites, draw.geometry.sprites) } } else { // Flush current batch @@ -1403,11 +1401,8 @@ function _flush_draws(cmd_buffer, pass, draws, camera, target) { sprites: [] } - if (draw.geometry && draw.geometry.sprites) { - for (var s of draw.geometry.sprites) { - current_batch.sprites.push(s) - } - } + if (draw.geometry && draw.geometry.sprites) + current_batch.sprites = array(current_batch.sprites, draw.geometry.sprites) } } else if (draw.cmd == 'draw_text') { // Flush current batch @@ -1438,7 +1433,7 @@ function _flush_draws(cmd_buffer, pass, draws, camera, target) { // Render mesh2d batch _render_mesh2d(cmd_buffer, pass, draw, camera, target) } - } + }) // Flush final batch if (current_batch) _render_batch(cmd_buffer, pass, current_batch, camera, target) @@ -1705,10 +1700,10 @@ function _render_mesh2d(cmd_buffer, pass, draw, camera, target) { // Count total vertices and indices var total_verts = 0 var total_indices = 0 - for (var m of meshes) { + arrfor(meshes, function(m) { if (m.verts) total_verts += length(m.verts) if (m.indices) total_indices += length(m.indices) - } + }) if (total_verts == 0 || total_indices == 0) return @@ -1718,14 +1713,14 @@ function _render_mesh2d(cmd_buffer, pass, draw, camera, target) { var index_data = blob_mod(total_indices * 2) var vertex_offset = 0 - for (var m of meshes) { + arrfor(meshes, function(m) { var verts = m.verts || [] var indices = m.indices || [] var opacity = m.opacity != null ? m.opacity : 1 var tint = m.tint || {r: 1, g: 1, b: 1, a: 1} // Write vertices - for (var v of verts) { + arrfor(verts, function(v) { vertex_data.wf(v.x) vertex_data.wf(v.y) vertex_data.wf(v.u != null ? v.u : 0) @@ -1738,15 +1733,15 @@ function _render_mesh2d(cmd_buffer, pass, draw, camera, target) { vertex_data.wf(g) vertex_data.wf(b) vertex_data.wf(a) - } + }) // Write indices (offset by current vertex count) - for (var idx of indices) { + arrfor(indices, function(idx) { index_data.w16(vertex_offset + idx) - } + }) vertex_offset += length(verts) - } + }) // Upload geometry var vb_size = total_verts * 32 @@ -2223,9 +2218,9 @@ function _do_shader_pass(cmd_buffer, cmd, get_swapchain_tex) { // Bind samplers var samplers = [{texture: input.texture, sampler: _sampler_linear}] if (cmd.extra_inputs) { - for (var extra of cmd.extra_inputs) { + arrfor(cmd.extra_inputs, function(extra) { samplers.push({texture: extra.texture, sampler: _sampler_linear}) - } + }) } pass.bind_fragment_samplers(0, samplers) diff --git a/tests/animation.ce b/tests/animation.ce index 2efadb3c..4ebb675d 100644 --- a/tests/animation.ce +++ b/tests/animation.ce @@ -29,7 +29,7 @@ var Anim = (() => { } function current(a){ return a.src.frames[a.idx].image; } - function updateAll(arr, dt){ for(def a of arr) update(a, dt); } + function updateAll(arr, dt){ arrfor(arr, function(a){ update(a, dt); }) } function draw(a, pos, opt, pipe){ draw2d.image(current(a), pos, 0, [0,0], [0,0], opt, pipe); } diff --git a/tests/camera.ce b/tests/camera.ce index 63a4f386..53fdd249 100644 --- a/tests/camera.ce +++ b/tests/camera.ce @@ -55,8 +55,7 @@ function loop() render.clear([22/255,120/255,194/255,255/255]) render.camera(camera) - for (var sp of sprite.sprites) - draw.image(sp.image, sp.rect) + arrfor(sprite.sprites, sp => draw.image(sp.image, sp.rect)) /* draw.line([[0,0],[100,50]]) draw.point([100,100]) diff --git a/tween.cm b/tween.cm index b5659479..4a680c18 100644 --- a/tween.cm +++ b/tween.cm @@ -17,9 +17,9 @@ function make_engine(default_clock) { if (current_time == null) { current_time = this.default_clock ? this.default_clock() : time.number() } - for (var tween of array(this.tweens)) { + arrfor(this.tweens, function(tween) { tween._update(current_time) - } + }) }, clear() { this.tweens = [] @@ -198,14 +198,14 @@ var TimelineProto = { this.engine.update(t) // Fire any events - for (var ev of this.events) { + arrfor(this.events, function(ev) { if (!ev.fired && t >= ev.time) { ev.fn() ev.fired = true } else if (ev.fired && t < ev.time) { ev.fired = false } - } + }) }, toJSON: function() {