From 027435d19375427f50a01de8c2cc00aabe3535cf Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 19 Jan 2026 18:57:25 -0600 Subject: [PATCH] rm for ... in --- action.cm | 35 ++++++++++------------------------- clay.cm | 34 ++++++++++++++++++++-------------- compositor.cm | 14 +++++++------- debug_imgui.cm | 20 ++++++++++---------- effects.cm | 12 +++++------- examples/bunnymark/main.ce | 1 - examples/pong/main.ce | 1 - film2d.cm | 9 ++++----- fx_graph.cm | 4 ++-- graphics.cm | 4 +++- input.cm | 16 +++++++--------- input/bindings.cm | 30 +++++++++++------------------- input/devices.cm | 10 ++++------ line2d.cm | 21 +-------------------- particles2d.cm | 4 +--- sdl_gpu.cm | 4 ++-- shape2d.cm | 23 +---------------------- sprite.cm | 4 +--- tests/camera_colorspace.ce | 10 +++++----- text2d.cm | 4 +--- tilemap2d.cm | 4 +--- tween.cm | 12 ++++++------ 22 files changed, 102 insertions(+), 174 deletions(-) diff --git a/action.cm b/action.cm index 4ffc3de5..5736a03e 100644 --- a/action.cm +++ b/action.cm @@ -170,9 +170,9 @@ action.current_device = 'keyboard' action.current_gamepad_type = null // Copy defaults -for (var key in default_action_map) { +arrfor(array(default_action_map), function(key) { action.action_map[key] = array(default_action_map[key]) -} +}) // Swipe‐recognizer state & tuning var swipe = { x0:null, y0:null, t0:0 } @@ -224,7 +224,7 @@ action.on_input = function(action_id, evt) // 3) Otherwise, find all mapped actions for this input var matched_actions = [] - for (var mapped_action in this.action_map) { + arrfor(array(this.action_map), mapped_action => { if (find(this.action_map[mapped_action], action_id) != null) { matched_actions.push(mapped_action) @@ -233,7 +233,7 @@ action.on_input = function(action_id, evt) else if (evt.released) this.down[mapped_action] = false } - } + }) // Send all matched actions (only if we found mappings - this means it's a raw input) if (length(matched_actions) > 0) { @@ -253,11 +253,11 @@ action.rebind_action = function(action_name, new_key) { if (!this.action_map[action_name]) return // Remove this key from all other actions - for (var act in this.action_map) { + arrfor(array(this.action_map), act => { var idx = find(this.action_map[act], new_key) if (idx != null) this.action_map[act].splice(idx, 1) - } + }) // Clear existing bindings for the current device from the target action var target_bindings = this.action_map[action_name] @@ -300,11 +300,7 @@ action.get_current_device_binding = function(action_name) { action.save_bindings = function() { try { - var bindings_data = {} - for (var key in this.action_map) { - bindings_data[key] = this.action_map[key] - } - io.slurpwrite('keybindings.json', json.encode(bindings_data)) + io.slurpwrite('keybindings.json', json.encode(this.action_map)) } catch(e) { log.console("Failed to save key bindings:", e) } @@ -314,12 +310,7 @@ action.load_bindings = function() { try { if (io.exists('keybindings.json')) { var data = io.slurp('keybindings.json') - var bindings = json.decode(data) - for (var key in bindings) { - if (this.action_map[key]) { - this.action_map[key] = bindings[key] - } - } + var bindings = object(json.decode(data), this.action_map) } } catch(e) { log.console("Failed to load key bindings:", e) @@ -327,25 +318,19 @@ action.load_bindings = function() { } action.reset_to_defaults = function() { - for (var key in default_action_map) { - this.action_map[key] = array(default_action_map[key]) - } + this.action_map = object(default_action_map) this.save_bindings() } return function() { var obj = meme(action) - obj.action_map = {} + obj.action_map = object(default_action_map) obj.display_names = action_display_names obj.is_rebinding = false obj.rebind_target = null obj.current_device = 'keyboard' obj.current_gamepad_type = null - // Copy defaults - for (var key in default_action_map) { - obj.action_map[key] = array(default_action_map[key]) - } obj.load_bindings() return obj } diff --git a/clay.cm b/clay.cm index 615e9ef2..01cb039e 100644 --- a/clay.cm +++ b/clay.cm @@ -213,7 +213,7 @@ function build_drawables(node, root_height, parent_abs_x, parent_abs_y, parent_s // --- Item Creation Helpers --- function process_configs(configs) { - var cfg = meme(base_config, ...configs) + var cfg = meme(base_config, configs) cfg.color = normalize_color(cfg.color, base_config.color) if (cfg.background_color) cfg.background_color = normalize_color(cfg.background_color, {r:1,g:1,b:1,a:1}) @@ -304,41 +304,47 @@ clay.zstack = function(configs, fn) { } // Leaf nodes -clay.image = function(path, ...configs) { +clay.image = function(path, configs) { var img = graphics.texture(path) - var c = {image: path} + var c = [{image: path}] var final_config = process_configs(configs) - if (!final_config.size && !final_config.behave) { + if (!final_config.size && !final_config.behave) c.size = {width: img.width, height: img.height} - } + + if (!is_array(configs)) configs = [configs] - push_node([c, ...configs], null) + push_node(array(c, configs), null) pop_node() } -clay.text = function(str, ...configs) { - var c = {text: str} +clay.text = function(str, configs) { + var c = [{text: str}] var final_config = process_configs(configs) if (!final_config.size && !final_config.behave) { c.size = {width: 100, height: 20} } + + if (!is_array(configs)) configs = [configs] - push_node([c, ...configs], null) + push_node(array(c, configs), null) pop_node() } -clay.rectangle = function(...configs) { +clay.rectangle = function(configs) { + if (!is_array(configs)) configs = [configs] push_node(configs, null) pop_node() } -clay.button = function(str, action, ...configs) { - var btn_config = { +clay.button = function(str, action, configs) { + var btn_config = [{ padding: 10, background_color: {r:0.3, g:0.3, b:0.4, a:1} - } + }] - clay.zstack([btn_config, ...configs], function() { + if (!is_array(configs)) configs = [configs] + + clay.zstack(array(btn_config, configs), function() { clay.text(str, {color: {r:1,g:1,b:1,a:1}}) }) } diff --git a/compositor.cm b/compositor.cm index f3f71dd7..e0a67a7b 100644 --- a/compositor.cm +++ b/compositor.cm @@ -56,13 +56,13 @@ function compile_plane(plane_config, ctx, group_effects) { // Build set of groups used as masks (these should not be drawn directly) var mask_groups = {} - for (var gname in group_effects) { + arrfor(array(group_effects), gname => { var effects = group_effects[gname].effects || [] for (var e = 0; e < length(effects); e++) { if (effects[e].type == 'mask' && effects[e].mask_group) mask_groups[effects[e].mask_group] = true } - } + }) // Get all sprites in this plane var all_sprites = film2d.query({plane: plane_name}) @@ -116,9 +116,9 @@ function compile_plane(plane_config, ctx, group_effects) { ctx.passes.push({type: 'clear', target: plane_target, color: plane_config.clear}) // Render each effect group to temp target, apply effects, composite back - for (var gname in effect_groups) { + arrfor(array(effect_groups), gname => { var eg = effect_groups[gname] - if (length(eg.sprites) == 0) continue + if (length(eg.sprites) == 0) return var group_target = ctx.alloc(res.width, res.height, gname + '_content') @@ -150,7 +150,7 @@ function compile_plane(plane_config, ctx, group_effects) { dest_size: res, blend: 'over' }) - } + }) // Render base sprites (no effects) if (length(base_sprites) > 0) { @@ -249,10 +249,10 @@ function apply_effect(ctx, effect, input, size, camera, hint, current_plane, gro compositor.execute = function(plan) { var cache = {} - for (var key in plan.targets) { + arrfor(array(plan.targets), key => { var spec = plan.targets[key] cache[key] = backend.get_or_create_target(spec.width, spec.height, key) - } + }) function resolve(t) { if (t == 'screen') return 'screen' diff --git a/debug_imgui.cm b/debug_imgui.cm index 515f8f34..0cc75366 100644 --- a/debug_imgui.cm +++ b/debug_imgui.cm @@ -248,7 +248,7 @@ function _render_node_inspector(imgui, node) { for (var i = 0; i < length(node.effects); i++) { var fx = node.effects[i] imgui.tree(fx.type, function() { - for (var k in fx) { + arrfor(array(fx), k => { if (k != 'type' && k != 'source') { var v = fx[k] if (is_number(v)) { @@ -257,7 +257,7 @@ function _render_node_inspector(imgui, node) { imgui.text(k + ": " + text(v)) } } - } + }) }) } } @@ -358,14 +358,14 @@ function _render_pass_inspector(imgui, pass) { if (pass.uniforms) { imgui.text("---") imgui.text("Uniforms:") - for (var k in pass.uniforms) { + arrfor(array(pass.uniforms), k => { var v = pass.uniforms[k] if (is_array(v)) { imgui.text(" " + k + ": [" + text(v, ", ") + "]") } else { imgui.text(" " + k + ": " + text(v)) } - } + }) } // Clear color @@ -407,14 +407,14 @@ function _render_effects_panel(imgui) { if (deff.params) { imgui.text("Parameters:") - for (var k in deff.params) { + arrfor(array(deff.params), k => { var p = deff.params[k] var info = k if (p.default != null) info += " = " + text(p.default) if (p.type) info += " (" + p.type + ")" if (p.required) info += " [required]" imgui.text(" " + info) - } + }) } }) } @@ -465,19 +465,19 @@ function _render_targets(imgui, plan) { imgui.text("Temporary Targets:") if (plan.targets) { - for (var key in plan.targets) { + arrfor(array(plan.targets), key => { var t = plan.targets[key] imgui.text(" " + key + ": " + text(t.width) + "x" + text(t.height)) - } + }) } imgui.text("---") imgui.text("Persistent Targets:") if (plan.persistent_targets) { - for (var key in plan.persistent_targets) { + arrfor(array(plan.persistent_targets), key => { var t = plan.persistent_targets[key] imgui.text(" " + key + ": " + text(t.width) + "x" + text(t.height )) - } + }) } }) } diff --git a/effects.cm b/effects.cm index 8f57e683..38fd2632 100644 --- a/effects.cm +++ b/effects.cm @@ -18,9 +18,7 @@ effects.get = function(name) { } effects.list = function() { - var names = [] - for (var k in _effects) names.push(k) - return names + return array(_effects) } // Built-in effect: Bloom @@ -367,11 +365,11 @@ effects.default_params = function(effect_type) { if (!deff || !deff.params) return {} var defaults = {} - for (var k in deff.params) { + arrfor(array(deff.params), k => { if (deff.params[k].default != null) { defaults[k] = deff.params[k].default } - } + }) return defaults } @@ -380,11 +378,11 @@ effects.validate_params = function(effect_type, params) { var deff = _effects[effect_type] if (!deff || !deff.params) return true - for (var k in deff.params) { + arrfor(array(deff.params), k => { if (deff.params[k].required && params[k] == null) { return false } - } + }) return true } diff --git a/examples/bunnymark/main.ce b/examples/bunnymark/main.ce index 1772b2f7..e2cac960 100644 --- a/examples/bunnymark/main.ce +++ b/examples/bunnymark/main.ce @@ -3,7 +3,6 @@ var render = use('render') var graphics = use('graphics') var sprite = use('sprite') var geom = use('geometry') -var input = use('controller') var config = use('config') var color = use('color') var random = use('random') diff --git a/examples/pong/main.ce b/examples/pong/main.ce index 01d26299..43a65c02 100644 --- a/examples/pong/main.ce +++ b/examples/pong/main.ce @@ -1,6 +1,5 @@ // main.js var draw = use('draw2d') -var input = use('controller') var config = use('config') var color = use('color') var random = use('random') diff --git a/film2d.cm b/film2d.cm index f33eda32..4d25d96d 100644 --- a/film2d.cm +++ b/film2d.cm @@ -227,10 +227,8 @@ film2d.query = function(selector) { } // All drawables - for (var id in registry) { - var d = registry[id] - if (d && d.visible != false) result.push(d) - } + var draws = array(registry, id => registry[id]) + result = array(result, filter(draws, d => d.visible != false)) return result } @@ -243,8 +241,9 @@ film2d.get_groups = function(id) { // List all known groups film2d.all_groups = function() { var groups = [] - for (var g in group_index) + arrfor(array(group_index), g => { if (length(group_index[g]) > 0) groups.push(g) + }) return groups } diff --git a/fx_graph.cm b/fx_graph.cm index fc0cff78..47e0310d 100644 --- a/fx_graph.cm +++ b/fx_graph.cm @@ -105,13 +105,13 @@ fx_graph.execute = function(backend) { fx_graph.resolve_inputs = function(params, node_outputs) { var resolved = {} - for (var key in params) { + arrfor(array(params), key => { var value = params[key] if (value && value.node_id != null) resolved[key] = node_outputs[value.node_id] else resolved[key] = value - } + }) return resolved } diff --git a/graphics.cm b/graphics.cm index 6b2cbb2a..8a12991e 100644 --- a/graphics.cm +++ b/graphics.cm @@ -178,7 +178,9 @@ function create_image(path){ return makeAnim(wrapFrames(raw.frames), !!raw.loop); def anims = {}; - arrfor(Object.entries(raw), function([name, anim]) { + var keys = array(raw) + arrfor(keys, function(name) { + var anim = raw[name] if(anim && is_array(anim.frames)) anims[name] = makeAnim(wrapFrames(anim.frames), !!anim.loop); else if(anim && anim.surface) diff --git a/input.cm b/input.cm index 6b91c6ac..98bc926e 100644 --- a/input.cm +++ b/input.cm @@ -47,20 +47,18 @@ function create_user(index, config) { var display_names = {} // Merge defaults with config - for (var k in default_action_map) { + arrfor(array(default_action_map), function(k) { action_map[k] = array(default_action_map[k]) display_names[k] = default_display_names[k] - } + }) if (config.action_map) { - for (var k in config.action_map) { + arrfor(array(config.action_map), function(k) { var val = config.action_map[k] action_map[k] = is_array(val) ? array(val) : [val] - } + }) } if (config.display_names) { - for (var k in config.display_names) { - display_names[k] = config.display_names[k] - } + arrfor(array(config.display_names), k => display_names[k] = config.display_names[k]) } var user = { @@ -160,11 +158,11 @@ function pick_user(canon) { if (user.active_device != canon.device_id) { // Release all held actions when switching device var old_down = user.router.down - for (var action in old_down) { + arrfor(array(old_down), action => { if (old_down[action]) { user.dispatch(action, { pressed: false, released: true, time: canon.time }) } - } + }) user.active_device = canon.device_id if (find(user.paired_devices, canon.device_id) == null) { diff --git a/input/bindings.cm b/input/bindings.cm index db758916..cc44e141 100644 --- a/input/bindings.cm +++ b/input/bindings.cm @@ -30,27 +30,21 @@ function make(defaults, display_names) { } // Copy defaults - for (var key in defaults) { + arrfor(array(defaults), function(key){ var val = defaults[key] bindings.action_map[key] = is_array(val) ? array(val) : [val] bindings._defaults[key] = array(bindings.action_map[key]) - } + }) // Get actions that match a control bindings.get_actions = function(control) { - var result = [] - for (var action in this.action_map) { - if (find(this.action_map[action], control) != null) { - result.push(action) - } - } - return result + return filter(array(this.action_map), action => find(this.action_map[action], control) != null) } // Get bindings for a specific device kind bindings.get_bindings_for_device = function(action, device_kind) { var all = this.action_map[action] || [] - return filter(all, function(control) { + return filter(all, control => { if (device_kind == 'keyboard') { return !starts_with(control, 'gamepad_') && !starts_with(control, 'swipe_') && !starts_with(control, 'touch') } @@ -139,10 +133,10 @@ function make(defaults, display_names) { if (!this.action_map[action]) return false // Remove from other actions - for (var act in this.action_map) { + arrfor(array(this.action_map), act => { var idx = search(this.action_map[act], new_control) if (idx >= 0) this.action_map[act].splice(idx, 1) - } + }) // Clear existing bindings for this device kind var target = this.action_map[action] @@ -184,11 +178,9 @@ function make(defaults, display_names) { try { if (io.exists(path)) { var data = json.decode(io.slurp(path)) - for (var key in data) { - if (this.action_map[key]) { - this.action_map[key] = data[key] - } - } + arrfor(array(data), key => { + if (this.action_map[key]) this.action_map[key] = data[key] + }) return true } } catch(e) {} @@ -197,9 +189,9 @@ function make(defaults, display_names) { // Reset to defaults bindings.reset = function() { - for (var key in this._defaults) { + arrfor(array(this._defaults), key => { this.action_map[key] = array(this._defaults[key]) - } + }) } return bindings diff --git a/input/devices.cm b/input/devices.cm index 4161d2a6..6cc2bed6 100644 --- a/input/devices.cm +++ b/input/devices.cm @@ -52,16 +52,14 @@ function gamepad_type(device_id) { // List all registered devices function list() { - return Object.keys(_devices) + return array(_devices) } // List devices of a specific kind function list_by_kind(kind) { - var result = [] - for (var id in _devices) { - if (_devices[id].kind == kind) result.push(id) - } - return result + return filter(array(_devices), function(id) { + return _devices[id].kind == kind + }) } return { diff --git a/line2d.cm b/line2d.cm index f83aa30e..45ac4af1 100644 --- a/line2d.cm +++ b/line2d.cm @@ -371,26 +371,7 @@ var defaults = { } function make_line(props) { - var data = {} - for (var k in defaults) { - var v = defaults[k] - if (is_object(v) && !is_array(v)) { - data[k] = {} - for (var kk in v) data[k][kk] = v[kk] - } else { - data[k] = v - } - } - - // Apply user props (deep merge for objects) - for (var k in props) { - var v = props[k] - if (is_object(v) && !is_array(v) && is_object(data[k])) { - for (var kk in v) data[k][kk] = v[kk] - } else { - data[k] = v - } - } + var data = object(defaults, props) // Ensure groups is array if (!data.groups) data.groups = [] diff --git a/particles2d.cm b/particles2d.cm index 4f9581bb..101499e0 100644 --- a/particles2d.cm +++ b/particles2d.cm @@ -128,9 +128,7 @@ var factory = function(props) { tint: {r: 1, g: 1, b: 1, a: 1} } - var data = {} - for(var k in defaults) data[k] = defaults[k] - for(var k in props) data[k] = props[k] + var data = object(defaults, props) var newparticles = meme(particles2d_proto, data) film2d.register(newparticles) diff --git a/sdl_gpu.cm b/sdl_gpu.cm index 2ae915c7..8289564b 100644 --- a/sdl_gpu.cm +++ b/sdl_gpu.cm @@ -1127,9 +1127,9 @@ function _preload_textures(commands) { }) // Load all textures - for (var path in paths) { + arrfor(array(paths, function(path) { sdl_gpu.get_texture(path) - } + })) } function _execute_commands(commands, window_size) { diff --git a/shape2d.cm b/shape2d.cm index 72c458ea..428e4b82 100644 --- a/shape2d.cm +++ b/shape2d.cm @@ -71,28 +71,7 @@ var defaults = { } function make_shape(shape_type, props) { - var data = {} - for (var k in defaults) { - var v = defaults[k] - if (is_object(v) && !is_array(v)) { - data[k] = {} - for (var kk in v) data[k][kk] = v[kk] - } else { - data[k] = v - } - } - - data.shape_type = shape_type - - // Apply user props (deep merge for objects) - for (var k in props) { - var v = props[k] - if (is_object(v) && !is_array(v) && is_object(data[k])) { - for (var kk in v) data[k][kk] = v[kk] - } else { - data[k] = v - } - } + var data = object(defaults, props) // Ensure groups is array if (!data.groups) data.groups = [] diff --git a/sprite.cm b/sprite.cm index d66966cb..c37b0fd6 100644 --- a/sprite.cm +++ b/sprite.cm @@ -33,9 +33,7 @@ return function(props) { visible: true } - var data = {} - for (var k in defaults) data[k] = defaults[k] - for (var k in props) data[k] = props[k] + var data = object(defaults, props) // Ensure groups is array if (!data.groups) data.groups = [] diff --git a/tests/camera_colorspace.ce b/tests/camera_colorspace.ce index 108f6037..40b0983a 100644 --- a/tests/camera_colorspace.ce +++ b/tests/camera_colorspace.ce @@ -27,14 +27,14 @@ for (var i = 0; i < length(formats); i++) { } log.console("\nFound colorspaces:"); -for (var cs in colorspaces) { - log.console(" " + cs + ": " + length(colorspaces[cs]) + " formats"); -} +arrfor(array(colorspaces), function(key) { + log.console(" " + key + ": " + length(colorspaces[key]) + " formats"); +}) // Try opening camera with different colorspaces log.console("\nTrying to open camera with different colorspaces..."); -for (var cs in colorspaces) { +arrfor(array(colorspaces), function(cs) { // Get first format for this colorspace var format = colorspaces[cs][0]; @@ -66,7 +66,7 @@ for (var cs in colorspaces) { // Just test first 3 colorspaces if (find(array(colorspaces), cs) >= 2) break; -} +}) log.console("\nColorspace test complete!"); $stop(); \ No newline at end of file diff --git a/text2d.cm b/text2d.cm index 5af65764..706e4302 100644 --- a/text2d.cm +++ b/text2d.cm @@ -43,9 +43,7 @@ return function(props) { outline_color: null } - var data = {} - for(var k in defaults) data[k] = defaults[k] - for(var k in props) data[k] = props[k] + var data = object(defaults, props) var newtext = meme(text2d_proto, data) film2d.register(newtext) diff --git a/tilemap2d.cm b/tilemap2d.cm index 4e77b6c5..e0fca815 100644 --- a/tilemap2d.cm +++ b/tilemap2d.cm @@ -54,9 +54,7 @@ return function(props) { tint: {r: 1, g: 1, b: 1, a: 1} } - var data = {} - for(var k in defaults) data[k] = defaults[k] - for(var k in props) data[k] = props[k] + var data = object(defaults, props) var newtilemap = meme(tilemap, data) film2d.register(newtilemap) diff --git a/tween.cm b/tween.cm index 4a680c18..8a70eceb 100644 --- a/tween.cm +++ b/tween.cm @@ -42,19 +42,19 @@ var TweenProto = { engine: null, to: function(props, duration, start_time) { - for (var key in props) { + arrfor(array(props), key => { var value = props[key] if (is_object(value)) { - for (var subkey in value) { + arrfor(array(value), subkey => { var flatKey = key + '.' + subkey this.startVals[flatKey] = this.obj[key] ? this.obj[key][subkey] : undefined this.endVals[flatKey] = value[subkey] - } + }) } else { this.startVals[key] = this.obj[key] this.endVals[key] = value } - } + }) this.duration = duration @@ -95,7 +95,7 @@ var TweenProto = { var t = min(max(elapsed / this.duration, 0), 1) var eased = this.easing(t) - for (var key in this.endVals) { + arrfor(array(this.endVals), key => { var start = this.startVals[key] var end = this.endVals[key] var value = start + (end - start) * eased @@ -113,7 +113,7 @@ var TweenProto = { } else { this.obj[key] = value } - } + }) if (t == 1 && this.engine) { this.onCompleteCallback()