funicular 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -0
  3. data/README.md +5 -5
  4. data/Rakefile +23 -15
  5. data/demo/test_chartjs.html +17 -17
  6. data/demo/test_component.html +15 -15
  7. data/demo/test_error_boundary.html +41 -41
  8. data/demo/test_router.html +57 -57
  9. data/demo/tic-tac-toe.html +29 -29
  10. data/docs/architecture.md +43 -30
  11. data/lib/funicular/compiler.rb +15 -16
  12. data/lib/funicular/helpers/picoruby_helper.rb +24 -1
  13. data/lib/funicular/ssr/runtime.rb +2 -0
  14. data/lib/funicular/ssr.rb +2 -1
  15. data/lib/funicular/testing/node_runner.rb +1 -1
  16. data/lib/funicular/vendor/mrbc/VERSION +1 -0
  17. data/lib/funicular/vendor/{picorbc/picorbc.js → mrbc/mrbc.js} +14 -1
  18. data/lib/funicular/vendor/mrbc/mrbc.wasm +0 -0
  19. data/lib/funicular/vendor/picoruby/VERSION +1 -1
  20. data/lib/funicular/vendor/picoruby/debug/init.iife.js +19 -4
  21. data/lib/funicular/vendor/picoruby/debug/picoruby.js +38 -23
  22. data/lib/funicular/vendor/picoruby/debug/picoruby.wasm +0 -0
  23. data/lib/funicular/vendor/picoruby/dist/init.iife.js +19 -4
  24. data/lib/funicular/vendor/picoruby/dist/picoruby.js +1 -1
  25. data/lib/funicular/vendor/picoruby/dist/picoruby.wasm +0 -0
  26. data/lib/funicular/vendor/picoruby-test-node/VERSION +1 -1
  27. data/lib/funicular/vendor/picoruby-test-node/picoruby.js +47 -23
  28. data/lib/funicular/vendor/picoruby-test-node/picoruby.wasm +0 -0
  29. data/lib/funicular/vendor/picoruby-test-node/picoruby.wasm.map +1 -1
  30. data/lib/funicular/version.rb +1 -1
  31. data/lib/generators/funicular/chat/templates/funicular_chat_component.rb.tt +47 -46
  32. data/lib/tasks/funicular.rake +1 -1
  33. data/minitest/commands_routes_test.rb +97 -0
  34. data/minitest/compiler_test.rb +195 -0
  35. data/minitest/configuration_test.rb +64 -0
  36. data/minitest/fixtures/funicular_app/components/greeting_component.rb +6 -6
  37. data/minitest/form_for_test.rb +106 -0
  38. data/minitest/funicular_test.rb +28 -2
  39. data/minitest/hydration_test.rb +2 -2
  40. data/minitest/middleware_test.rb +154 -0
  41. data/minitest/picoruby_helper_test.rb +139 -0
  42. data/minitest/route_parser_test.rb +139 -0
  43. data/minitest/schema_test.rb +23 -0
  44. data/minitest/ssr_test.rb +57 -0
  45. data/minitest/support/rails_stub.rb +59 -0
  46. data/minitest/test_helper.rb +20 -0
  47. data/minitest/testing_test.rb +267 -0
  48. data/minitest/view_context_test.rb +101 -0
  49. data/mrblib/component.rb +210 -229
  50. data/mrblib/debug.rb +7 -6
  51. data/mrblib/differ.rb +3 -1
  52. data/mrblib/error_boundary.rb +19 -27
  53. data/mrblib/form_builder.rb +34 -24
  54. data/mrblib/funicular.rb +2 -1
  55. data/mrblib/html_serializer.rb +14 -13
  56. data/mrblib/http.rb +12 -1
  57. data/mrblib/patcher.rb +16 -9
  58. data/mrblib/router.rb +9 -5
  59. data/mrblib/runtime.rb +28 -0
  60. data/mrblib/styles.rb +13 -17
  61. data/mrblib/vdom.rb +92 -9
  62. data/mrblib/view_context.rb +135 -0
  63. data/sig/component.rbs +34 -85
  64. data/sig/error_boundary.rbs +4 -4
  65. data/sig/form_builder.rbs +2 -1
  66. data/sig/html_serializer.rbs +2 -1
  67. data/sig/http.rbs +1 -0
  68. data/sig/patcher.rbs +1 -1
  69. data/sig/router.rbs +2 -13
  70. data/sig/runtime.rbs +13 -0
  71. data/sig/styles.rbs +3 -4
  72. data/sig/vdom.rbs +11 -2
  73. data/sig/view_context.rbs +47 -0
  74. metadata +19 -5
  75. data/lib/funicular/vendor/picorbc/VERSION +0 -1
  76. data/lib/funicular/vendor/picorbc/picorbc.wasm +0 -0
@@ -5411,20 +5411,22 @@ function checkIncomingModuleAPI() {
5411
5411
  ignoredModuleProp('loadSplitModule');
5412
5412
  }
5413
5413
  var ASM_CONSTS = {
5414
- 2635911: ($0) => { globalThis.picorubyRefs[$0] = null; },
5415
- 2635951: ($0) => { globalThis.picorubyRefs[$0] = true; },
5416
- 2635991: ($0) => { globalThis.picorubyRefs[$0] = false; },
5417
- 2636032: ($0, $1) => { globalThis.picorubyRefs[$0] = $1; },
5418
- 2636070: ($0, $1) => { globalThis.picorubyRefs[$0] = $1; },
5419
- 2636108: ($0, $1, $2) => { const str = UTF8ToString($1, $2); globalThis.picorubyRefs[$0] = str; },
5420
- 2636181: ($0, $1) => { const arr = globalThis.picorubyRefs[$0]; const elem = globalThis.picorubyRefs[$1]; arr.push(elem); delete globalThis.picorubyRefs[$1]; },
5421
- 2636320: ($0, $1, $2) => { const obj = globalThis.picorubyRefs[$0]; const key = UTF8ToString($1); const val = globalThis.picorubyRefs[$2]; obj[key] = val; delete globalThis.picorubyRefs[$2]; }
5414
+ 2652134: ($0) => { globalThis.picorubyRefs[$0] = null; },
5415
+ 2652174: ($0) => { globalThis.picorubyRefs[$0] = true; },
5416
+ 2652214: ($0) => { globalThis.picorubyRefs[$0] = false; },
5417
+ 2652255: ($0, $1) => { globalThis.picorubyRefs[$0] = $1; },
5418
+ 2652293: ($0, $1) => { globalThis.picorubyRefs[$0] = $1; },
5419
+ 2652331: ($0, $1, $2) => { const str = UTF8ToString($1, $2); globalThis.picorubyRefs[$0] = str; },
5420
+ 2652404: ($0, $1) => { const arr = globalThis.picorubyRefs[$0]; const elem = globalThis.picorubyRefs[$1]; arr.push(elem); delete globalThis.picorubyRefs[$1]; },
5421
+ 2652543: ($0, $1, $2) => { const obj = globalThis.picorubyRefs[$0]; const key = UTF8ToString($1); const val = globalThis.picorubyRefs[$2]; obj[key] = val; delete globalThis.picorubyRefs[$2]; }
5422
5422
  };
5423
5423
  function ble_dataview_length(ref_id) { try { const dv = globalThis.picorubyRefs[ref_id]; if (dv && dv.byteLength !== undefined) { return dv.byteLength; } return 0; } catch(e) { console.error('ble_dataview_length failed:', e); return 0; } }
5424
5424
  function ble_dataview_read(ref_id,out_buf,max_len) { try { const dv = globalThis.picorubyRefs[ref_id]; if (!dv) return 0; const len = Math.min(dv.byteLength, max_len); for (let i = 0; i < len; i++) { HEAPU8[out_buf + i] = dv.getUint8(i); } return len; } catch(e) { console.error('ble_dataview_read failed:', e); return 0; } }
5425
5425
  function ble_create_uint8array(data,length) { try { const buffer = new Uint8Array(HEAPU8.buffer, data, length); const copy = new Uint8Array(buffer); const refId = globalThis.picorubyRefs.push(copy) - 1; return refId; } catch(e) { console.error('ble_create_uint8array failed:', e); return -1; } }
5426
5426
  function ble_set_notify_handler(char_ref_id,callback_id) { try { const char_obj = globalThis.picorubyRefs[char_ref_id]; char_obj.addEventListener('characteristicvaluechanged', (event) => { const dataview = event.target.value; const len = dataview.byteLength; const ptr = _malloc(len); for (let i = 0; i < len; i++) { HEAPU8[ptr + i] = dataview.getUint8(i); } ccall( 'ble_notify_callback', 'void', ['number', 'number', 'number'], [callback_id, ptr, len] ); _free(ptr); }); } catch(e) { console.error('ble_set_notify_handler failed:', e); } }
5427
5427
  function init_js_refs() { if (typeof globalThis.picorubyRefs === 'undefined') { globalThis.picorubyRefs = []; const rootObject = typeof window !== 'undefined' ? window : globalThis; globalThis.picorubyRefs.push(rootObject); } if (typeof window !== 'undefined' && typeof window._js_remove_event_listener_wrapper === 'undefined') { window._js_remove_event_listener_wrapper = function(callback_id) { if (!globalThis.picorubyEventHandlers) return false; const info = globalThis.picorubyEventHandlers[callback_id]; if (!info) return false; info.target.removeEventListener(info.type, info.handler); delete globalThis.picorubyEventHandlers[callback_id]; return true; }; } }
5428
+ function js_last_error_length() { const message = globalThis.picorubyLastError; return message ? lengthBytesUTF8(message) : 0; }
5429
+ function js_copy_last_error(buffer,buffer_size) { const message = globalThis.picorubyLastError; if (!message) { globalThis.picorubyLastError = null; return; } stringToUTF8(message, buffer, buffer_size); globalThis.picorubyLastError = null; }
5428
5430
  function is_array_like(ref_id) { const obj = globalThis.picorubyRefs[ref_id]; const isNodeList = typeof NodeList !== 'undefined' && obj instanceof NodeList; const isHTMLCollection = typeof HTMLCollection !== 'undefined' && obj instanceof HTMLCollection; return isNodeList || isHTMLCollection || (typeof obj === 'object' && obj !== null && 'length' in obj && typeof obj.length === 'number'); }
5429
5431
  function get_element(ref_id,index) { try { const nodeList = globalThis.picorubyRefs[ref_id]; const element = nodeList[index]; if (element === undefined) { return -1; } const newRefId = globalThis.picorubyRefs.push(element) - 1; return newRefId; } catch(e) { return -1; } }
5430
5432
  function set_property(ref_id,key,value,value_len) { try { if (!globalThis.picorubyRefs || ref_id >= globalThis.picorubyRefs.length) { console.error('Invalid reference ID:', ref_id); return false; } const obj = globalThis.picorubyRefs[ref_id]; if (!obj) { console.error('Object not found for ref_id:', ref_id); return false; } const property = UTF8ToString(key); const val = UTF8ToString(value, value_len); obj[property] = val; return true; } catch(e) { console.error('Error in set_property:', e); return false; } }
@@ -5443,19 +5445,19 @@ function get_number_value(ref_id) { return globalThis.picorubyRefs[ref_id]; }
5443
5445
  function get_string_value_length(ref_id) { const str = globalThis.picorubyRefs[ref_id]; return lengthBytesUTF8(str); }
5444
5446
  function copy_string_value(ref_id,buffer,buffer_size) { const str = globalThis.picorubyRefs[ref_id]; stringToUTF8(str, buffer, buffer_size); }
5445
5447
  function get_length(ref_id) { try { const obj = globalThis.picorubyRefs[ref_id]; return obj.length || 0; } catch(e) { return -1; } }
5446
- function call_method(ref_id,method,arg,arg_len) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argString = UTF8ToString(arg, arg_len); let result; if (methodName === 'new') { result = new obj(argString); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argString); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5447
- function call_method_no_arg(ref_id,method) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } let result = func.call(obj); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5448
- function call_method_no_return(ref_id,method) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; if (typeof func === 'function') { func.call(obj); } } catch(e) { console.error('call_method_no_return error:', e); } }
5449
- function call_method_int(ref_id,method,arg) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; let result; if (methodName === 'new') { result = new obj(arg); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, arg); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5450
- function call_method_str(ref_id,method,arg1,arg1_len,arg2,arg2_len) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argString1 = UTF8ToString(arg1, arg1_len); const argString2 = UTF8ToString(arg2, arg2_len); let result; if (methodName === 'new') { result = new obj(argString1, argString2); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argString1, argString2); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5451
- function call_method_with_ref(ref_id,method,arg_ref_id) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argObj = globalThis.picorubyRefs[arg_ref_id]; let result; if (methodName === 'new') { result = new obj(argObj); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argObj); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5452
- function call_method_with_ref_ref(ref_id,method,arg_ref_1_id,arg_ref_2_id) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argObj1 = globalThis.picorubyRefs[arg_ref_1_id]; const argObj2 = globalThis.picorubyRefs[arg_ref_2_id]; let result; if (methodName === 'new') { result = new obj(argObj1, argObj2); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argObj1, argObj2); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5453
- function call_method_with_ref_str_str(ref_id,method,arg1_ref_id,arg2_str,arg2_len,arg3_str,arg3_len) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argObj1 = globalThis.picorubyRefs[arg1_ref_id]; const argString2 = UTF8ToString(arg2_str, arg2_len); const argString3 = UTF8ToString(arg3_str, arg3_len); if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } let result = func.call(obj, argObj1, argString2, argString3); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error('Error in call_method_with_ref_str_str:', e); return -1; } }
5454
- function call_method_with_args(ref_id,method,args_json,args_json_len) { try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } const argsStr = UTF8ToString(args_json, args_json_len); const argsData = JSON.parse(argsStr); if (!Array.isArray(argsData)) { console.error('args_json must be a JSON array'); return -1; } const args = argsData.map(arg => { switch (arg.type) { case 'string': return arg.value; case 'integer': return arg.value; case 'float': return arg.value; case 'boolean': return arg.value; case 'ref': return globalThis.picorubyRefs[arg.value]; case 'nil': return null; default: console.error('Unknown argument type:', arg.type); return null; } }); const result = func.call(obj, ...args); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error('Error in call_method_with_args:', e); return -1; } }
5455
- function call_constructor_with_args(ref_id,args_json,args_json_len) { try { const ctor = globalThis.picorubyRefs[ref_id]; if (typeof ctor !== 'function') { console.error('Object is not a constructor function'); return -1; } const argsStr = UTF8ToString(args_json, args_json_len); const argsData = JSON.parse(argsStr); if (!Array.isArray(argsData)) { console.error('args_json must be a JSON array'); return -1; } const args = argsData.map(arg => { switch (arg.type) { case 'string': return arg.value; case 'integer': return arg.value; case 'float': return arg.value; case 'boolean': return arg.value; case 'ref': return globalThis.picorubyRefs[arg.value]; case 'nil': return null; default: console.error('Unknown argument type:', arg.type); return null; } }); const result = new ctor(...args); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error('Error in call_constructor_with_args:', e); return -1; } }
5456
- function js_function_apply_args(func_ref_id,args_json,args_json_len) { try { const fn = globalThis.picorubyRefs[func_ref_id]; if (typeof fn !== 'function') { console.error('js_function_apply_args: not a function'); return -1; } const argsData = JSON.parse(UTF8ToString(args_json, args_json_len)); if (!Array.isArray(argsData)) { console.error('js_function_apply_args: args_json must be a JSON array'); return -1; } const args = argsData.map(arg => { switch (arg.type) { case 'string': case 'integer': case 'float': case 'boolean': return arg.value; case 'ref': return globalThis.picorubyRefs[arg.value]; case 'nil': return null; default: return null; } }); const result = fn(...args); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error('Error in js_function_apply_args:', e); return -1; } }
5457
- function call_fetch_with_json_options(ref_id,url,options_json) { try { const obj = globalThis.picorubyRefs[ref_id]; const urlStr = UTF8ToString(url); const optionsStr = UTF8ToString(options_json); const options = JSON.parse(optionsStr); const result = obj.fetch(urlStr, options); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { console.error(e); return -1; } }
5458
- function setup_promise_handler(promise_id,callback_id,mrb_ptr,task_ptr) { const promise = globalThis.picorubyRefs[promise_id]; promise.then( (result) => { const resultId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); ccall( 'resume_promise_task', 'void', ['number', 'number', 'number', 'number'], [mrb_ptr, task_ptr, callback_id, resultId] ); } ); }
5448
+ function call_method(ref_id,method,arg,arg_len) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argString = UTF8ToString(arg, arg_len); let result; if (methodName === 'new') { result = new obj(argString); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argString); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5449
+ function call_method_no_arg(ref_id,method) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } let result = func.call(obj); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5450
+ function call_method_no_return(ref_id,method) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; if (typeof func === 'function') { func.call(obj); } } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); } }
5451
+ function call_method_int(ref_id,method,arg) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; let result; if (methodName === 'new') { result = new obj(arg); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, arg); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5452
+ function call_method_str(ref_id,method,arg1,arg1_len,arg2,arg2_len) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argString1 = UTF8ToString(arg1, arg1_len); const argString2 = UTF8ToString(arg2, arg2_len); let result; if (methodName === 'new') { result = new obj(argString1, argString2); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argString1, argString2); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5453
+ function call_method_with_ref(ref_id,method,arg_ref_id) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argObj = globalThis.picorubyRefs[arg_ref_id]; let result; if (methodName === 'new') { result = new obj(argObj); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argObj); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5454
+ function call_method_with_ref_ref(ref_id,method,arg_ref_1_id,arg_ref_2_id) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argObj1 = globalThis.picorubyRefs[arg_ref_1_id]; const argObj2 = globalThis.picorubyRefs[arg_ref_2_id]; let result; if (methodName === 'new') { result = new obj(argObj1, argObj2); } else { if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } result = func.call(obj, argObj1, argObj2); } const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5455
+ function call_method_with_ref_str_str(ref_id,method,arg1_ref_id,arg2_str,arg2_len,arg3_str,arg3_len) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; const argObj1 = globalThis.picorubyRefs[arg1_ref_id]; const argString2 = UTF8ToString(arg2_str, arg2_len); const argString3 = UTF8ToString(arg3_str, arg3_len); if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } let result = func.call(obj, argObj1, argString2, argString3); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5456
+ function call_method_with_args(ref_id,method,args_json,args_json_len) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const methodName = UTF8ToString(method); const func = obj[methodName]; if (typeof func !== 'function') { console.error('Method not found or not a function:', methodName); return -1; } const argsStr = UTF8ToString(args_json, args_json_len); const argsData = JSON.parse(argsStr); if (!Array.isArray(argsData)) { console.error('args_json must be a JSON array'); return -1; } const args = argsData.map(arg => { switch (arg.type) { case 'string': return arg.value; case 'integer': return arg.value; case 'float': return arg.value; case 'boolean': return arg.value; case 'ref': return globalThis.picorubyRefs[arg.value]; case 'nil': return null; default: console.error('Unknown argument type:', arg.type); return null; } }); const result = func.call(obj, ...args); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5457
+ function call_constructor_with_args(ref_id,args_json,args_json_len) { globalThis.picorubyLastError = null; try { const ctor = globalThis.picorubyRefs[ref_id]; if (typeof ctor !== 'function') { console.error('Object is not a constructor function'); return -1; } const argsStr = UTF8ToString(args_json, args_json_len); const argsData = JSON.parse(argsStr); if (!Array.isArray(argsData)) { console.error('args_json must be a JSON array'); return -1; } const args = argsData.map(arg => { switch (arg.type) { case 'string': return arg.value; case 'integer': return arg.value; case 'float': return arg.value; case 'boolean': return arg.value; case 'ref': return globalThis.picorubyRefs[arg.value]; case 'nil': return null; default: console.error('Unknown argument type:', arg.type); return null; } }); const result = new ctor(...args); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5458
+ function js_function_apply_args(func_ref_id,args_json,args_json_len) { globalThis.picorubyLastError = null; try { const fn = globalThis.picorubyRefs[func_ref_id]; if (typeof fn !== 'function') { console.error('js_function_apply_args: not a function'); return -1; } const argsData = JSON.parse(UTF8ToString(args_json, args_json_len)); if (!Array.isArray(argsData)) { console.error('js_function_apply_args: args_json must be a JSON array'); return -1; } const args = argsData.map(arg => { switch (arg.type) { case 'string': case 'integer': case 'float': case 'boolean': return arg.value; case 'ref': return globalThis.picorubyRefs[arg.value]; case 'nil': return null; default: return null; } }); const result = fn(...args); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5459
+ function call_fetch_with_json_options(ref_id,url,options_json) { globalThis.picorubyLastError = null; try { const obj = globalThis.picorubyRefs[ref_id]; const urlStr = UTF8ToString(url); const optionsStr = UTF8ToString(options_json); const options = JSON.parse(optionsStr); const result = obj.fetch(urlStr, options); const newRefId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); return newRefId; } catch(e) { globalThis.picorubyLastError = e && typeof e.message === 'string' ? e.message : String(e); return -1; } }
5460
+ function setup_promise_handler(promise_id,callback_id,mrb_ptr,task_ptr) { const promise = globalThis.picorubyRefs[promise_id]; promise.then( (result) => { const resultId = globalThis.picorubyRefs.length; globalThis.picorubyRefs.push(result); ccall( 'resume_promise_task', 'void', ['number', 'number', 'number', 'number'], [mrb_ptr, task_ptr, callback_id, resultId] ); } ).catch( (error) => { const message = error && typeof error.message === 'string' ? error.message : String(error); const size = lengthBytesUTF8(message) + 1; const errorPtr = _malloc(size); stringToUTF8(message, errorPtr, size); ccall( 'resume_promise_error_task', 'void', ['number', 'number', 'number', 'number'], [mrb_ptr, task_ptr, callback_id, errorPtr] ); } ); }
5459
5461
  function js_add_event_listener(ref_id,callback_id,event_type) { const target = globalThis.picorubyRefs[ref_id]; const type = UTF8ToString(event_type); const handler = (event) => { if (!globalThis.picorubyEventHandlers || !globalThis.picorubyEventHandlers[callback_id]) { return; } if (type === 'submit' || (type === 'click' && target.tagName === 'A')) { event.preventDefault(); } const eventRefId = globalThis.picorubyRefs.push(event) - 1; ccall( 'call_ruby_callback', 'void', ['number', 'number'], [callback_id, eventRefId] ); }; target.addEventListener(type, handler); if (!globalThis.picorubyEventHandlers) { globalThis.picorubyEventHandlers = {}; } globalThis.picorubyEventHandlers[callback_id] = { target, type, handler }; }
5460
5462
  function js_register_generic_callback(callback_id,callback_name) { const name = UTF8ToString(callback_name); if (!globalThis.picorubyGenericCallbacks) { globalThis.picorubyGenericCallbacks = {}; } globalThis.picorubyGenericCallbacks[name] = function(...args) { const argRefIds = args.map(arg => { const refId = globalThis.picorubyRefs.push(arg) - 1; return refId; }); const argRefIdsPtr = _malloc(argRefIds.length * 4); for (let i = 0; i < argRefIds.length; i++) { HEAP32[(argRefIdsPtr >> 2) + i] = argRefIds[i]; } const resultRefId = ccall( 'call_ruby_callback_sync_generic', 'number', ['number', 'number', 'number'], [callback_id, argRefIdsPtr, argRefIds.length] ); _free(argRefIdsPtr); if (resultRefId >= 0 && resultRefId < globalThis.picorubyRefs.length) { return globalThis.picorubyRefs[resultRefId]; } return undefined; }; }
5461
5463
  function js_create_callback_function(callback_id) { try { const fn = function(...args) { const argRefIds = args.map(arg => globalThis.picorubyRefs.push(arg) - 1); const argRefIdsPtr = _malloc(argRefIds.length * 4); for (let i = 0; i < argRefIds.length; i++) { HEAP32[(argRefIdsPtr >> 2) + i] = argRefIds[i]; } const resultRefId = ccall( 'call_ruby_callback_sync_generic', 'number', ['number', 'number', 'number'], [callback_id, argRefIdsPtr, argRefIds.length] ); _free(argRefIdsPtr); if (resultRefId >= 0 && resultRefId < globalThis.picorubyRefs.length) { return globalThis.picorubyRefs[resultRefId]; } return undefined; }; return globalThis.picorubyRefs.push(fn) - 1; } catch (e) { console.error('js_create_callback_function failed:', e); return -1; } }
@@ -5505,7 +5507,7 @@ function serial_capture_start(ref_id) { try { const port = globalThis.picorubyRe
5505
5507
  function serial_binary_capture_start(ref_id) { try { const port = globalThis.picorubyRefs[ref_id]; if (!port) return; if (!globalThis.picorubySerialBinCap) { const buffers = new WeakMap(); const active = new WeakSet(); const MAX_BYTES = 256 * 1024; globalThis.picorubySerialBinCap = { start(p) { buffers.set(p, { chunks: [], totalBytes: 0 }); active.add(p); }, isActive(p) { return active.has(p); }, append(p, value) { const buf = buffers.get(p); if (!buf) return; const copy = new Uint8Array(value); buf.chunks.push(copy); buf.totalBytes += copy.length; while (buf.totalBytes > MAX_BYTES && buf.chunks.length > 1) { const removed = buf.chunks.shift(); buf.totalBytes -= removed.length; } }, read(p, outPtr, maxBytes) { const buf = buffers.get(p); if (!buf || buf.totalBytes === 0) return 0; let written = 0; while (written < maxBytes && buf.chunks.length > 0) { const chunk = buf.chunks[0]; const needed = maxBytes - written; if (chunk.length <= needed) { HEAPU8.set(chunk, outPtr + written); written += chunk.length; buf.chunks.shift(); } else { HEAPU8.set(chunk.subarray(0, needed), outPtr + written); buf.chunks[0] = chunk.subarray(needed); written += needed; } } buf.totalBytes -= written; return written; }, stop(p) { active.delete(p); buffers.delete(p); }, }; } globalThis.picorubySerialBinCap.start(port); } catch (e) { console.error('serial_binary_capture_start failed:', e); } }
5506
5508
  function serial_binary_capture_read(ref_id,out_buf,max_bytes) { try { const port = globalThis.picorubyRefs[ref_id]; if (!port || !globalThis.picorubySerialBinCap) return 0; return globalThis.picorubySerialBinCap.read(port, out_buf, max_bytes); } catch (e) { console.error('serial_binary_capture_read failed:', e); return 0; } }
5507
5509
  function serial_binary_capture_stop(ref_id) { try { const port = globalThis.picorubyRefs[ref_id]; if (!port || !globalThis.picorubySerialBinCap) return; globalThis.picorubySerialBinCap.stop(port); } catch (e) { console.error('serial_binary_capture_stop failed:', e); } }
5508
- function serial_capture_get(ref_id,stop) { try { const port = globalThis.picorubyRefs[ref_id]; if (!port || !globalThis.picorubySerialCapture) { return globalThis.picorubyRefs.push("") - 1; } const cap = globalThis.picorubySerialCapture; const out = stop ? cap.stop(port) : cap.peek(port); return globalThis.picorubyRefs.push(out) - 1; } catch (e) { console.error('serial_capture_get failed:', e); return globalThis.picorubyRefs.push("") - 1; } }
5510
+ function serial_capture_copy(ref_id,stop,out_buf,max_bytes) { try { const port = globalThis.picorubyRefs[ref_id]; if (!port || !globalThis.picorubySerialCapture) { if (max_bytes > 0) HEAPU8[out_buf] = 0; return 0; } const cap = globalThis.picorubySerialCapture; const out = stop ? cap.stop(port) : cap.peek(port); const bytes = new TextEncoder().encode(out); const n = Math.min(bytes.length, Math.max(0, max_bytes - 1)); if (n > 0) { HEAPU8.set(bytes.subarray(0, n), out_buf); } if (max_bytes > 0) { HEAPU8[out_buf + n] = 0; } return n; } catch (e) { console.error('serial_capture_copy failed:', e); if (max_bytes > 0) HEAPU8[out_buf] = 0; return 0; } }
5509
5511
  function ws_new(url) { try { const ws = new WebSocket(UTF8ToString(url)); const refId = globalThis.picorubyRefs.push(ws) - 1; return refId; } catch(e) { console.error('WebSocket creation failed:', e); return -1; } }
5510
5512
  function ws_send(ref_id,data) { try { const ws = globalThis.picorubyRefs[ref_id]; if (ws && ws.readyState === WebSocket.OPEN) { ws.send(UTF8ToString(data)); } } catch(e) { console.error('WebSocket send failed:', e); } }
5511
5513
  function ws_send_binary(ref_id,data,length) { try { const ws = globalThis.picorubyRefs[ref_id]; if (ws && ws.readyState === WebSocket.OPEN) { const buffer = new Uint8Array(HEAPU8.buffer, data, length); const copy = new Uint8Array(buffer); ws.send(copy.buffer); } } catch(e) { console.error('WebSocket send_binary failed:', e); } }
@@ -5538,10 +5540,13 @@ var _mrb_debug_get_callstack = Module['_mrb_debug_get_callstack'] = makeInvalidE
5538
5540
  var _call_ruby_callback = Module['_call_ruby_callback'] = makeInvalidEarlyAccess('_call_ruby_callback');
5539
5541
  var _call_ruby_callback_oneshot = Module['_call_ruby_callback_oneshot'] = makeInvalidEarlyAccess('_call_ruby_callback_oneshot');
5540
5542
  var _resume_promise_task = Module['_resume_promise_task'] = makeInvalidEarlyAccess('_resume_promise_task');
5543
+ var _resume_promise_error_task = Module['_resume_promise_error_task'] = makeInvalidEarlyAccess('_resume_promise_error_task');
5541
5544
  var _resume_binary_task = Module['_resume_binary_task'] = makeInvalidEarlyAccess('_resume_binary_task');
5542
5545
  var _call_ruby_callback_sync_generic = Module['_call_ruby_callback_sync_generic'] = makeInvalidEarlyAccess('_call_ruby_callback_sync_generic');
5543
5546
  var _mrb_tick_wasm = Module['_mrb_tick_wasm'] = makeInvalidEarlyAccess('_mrb_tick_wasm');
5544
5547
  var _mrb_run_step = Module['_mrb_run_step'] = makeInvalidEarlyAccess('_mrb_run_step');
5548
+ var _mrb_run_step_status = Module['_mrb_run_step_status'] = makeInvalidEarlyAccess('_mrb_run_step_status');
5549
+ var _mrb_gc_scheduler_pending_wasm = Module['_mrb_gc_scheduler_pending_wasm'] = makeInvalidEarlyAccess('_mrb_gc_scheduler_pending_wasm');
5545
5550
  var _picorb_init = Module['_picorb_init'] = makeInvalidEarlyAccess('_picorb_init');
5546
5551
  var _picorb_create_task = Module['_picorb_create_task'] = makeInvalidEarlyAccess('_picorb_create_task');
5547
5552
  var _picorb_create_task_with_filename = Module['_picorb_create_task_with_filename'] = makeInvalidEarlyAccess('_picorb_create_task_with_filename');
@@ -5583,10 +5588,13 @@ function assignWasmExports(wasmExports) {
5583
5588
  assert(typeof wasmExports['call_ruby_callback'] != 'undefined', 'missing Wasm export: call_ruby_callback');
5584
5589
  assert(typeof wasmExports['call_ruby_callback_oneshot'] != 'undefined', 'missing Wasm export: call_ruby_callback_oneshot');
5585
5590
  assert(typeof wasmExports['resume_promise_task'] != 'undefined', 'missing Wasm export: resume_promise_task');
5591
+ assert(typeof wasmExports['resume_promise_error_task'] != 'undefined', 'missing Wasm export: resume_promise_error_task');
5586
5592
  assert(typeof wasmExports['resume_binary_task'] != 'undefined', 'missing Wasm export: resume_binary_task');
5587
5593
  assert(typeof wasmExports['call_ruby_callback_sync_generic'] != 'undefined', 'missing Wasm export: call_ruby_callback_sync_generic');
5588
5594
  assert(typeof wasmExports['mrb_tick_wasm'] != 'undefined', 'missing Wasm export: mrb_tick_wasm');
5589
5595
  assert(typeof wasmExports['mrb_run_step'] != 'undefined', 'missing Wasm export: mrb_run_step');
5596
+ assert(typeof wasmExports['mrb_run_step_status'] != 'undefined', 'missing Wasm export: mrb_run_step_status');
5597
+ assert(typeof wasmExports['mrb_gc_scheduler_pending_wasm'] != 'undefined', 'missing Wasm export: mrb_gc_scheduler_pending_wasm');
5590
5598
  assert(typeof wasmExports['picorb_init'] != 'undefined', 'missing Wasm export: picorb_init');
5591
5599
  assert(typeof wasmExports['picorb_create_task'] != 'undefined', 'missing Wasm export: picorb_create_task');
5592
5600
  assert(typeof wasmExports['picorb_create_task_with_filename'] != 'undefined', 'missing Wasm export: picorb_create_task_with_filename');
@@ -5624,10 +5632,13 @@ function assignWasmExports(wasmExports) {
5624
5632
  _call_ruby_callback = Module['_call_ruby_callback'] = createExportWrapper('call_ruby_callback', 2);
5625
5633
  _call_ruby_callback_oneshot = Module['_call_ruby_callback_oneshot'] = createExportWrapper('call_ruby_callback_oneshot', 2);
5626
5634
  _resume_promise_task = Module['_resume_promise_task'] = createExportWrapper('resume_promise_task', 4);
5635
+ _resume_promise_error_task = Module['_resume_promise_error_task'] = createExportWrapper('resume_promise_error_task', 4);
5627
5636
  _resume_binary_task = Module['_resume_binary_task'] = createExportWrapper('resume_binary_task', 5);
5628
5637
  _call_ruby_callback_sync_generic = Module['_call_ruby_callback_sync_generic'] = createExportWrapper('call_ruby_callback_sync_generic', 3);
5629
5638
  _mrb_tick_wasm = Module['_mrb_tick_wasm'] = createExportWrapper('mrb_tick_wasm', 0);
5630
5639
  _mrb_run_step = Module['_mrb_run_step'] = createExportWrapper('mrb_run_step', 0);
5640
+ _mrb_run_step_status = Module['_mrb_run_step_status'] = createExportWrapper('mrb_run_step_status', 0);
5641
+ _mrb_gc_scheduler_pending_wasm = Module['_mrb_gc_scheduler_pending_wasm'] = createExportWrapper('mrb_gc_scheduler_pending_wasm', 0);
5631
5642
  _picorb_init = Module['_picorb_init'] = createExportWrapper('picorb_init', 0);
5632
5643
  _picorb_create_task = Module['_picorb_create_task'] = createExportWrapper('picorb_create_task', 1);
5633
5644
  _picorb_create_task_with_filename = Module['_picorb_create_task_with_filename'] = createExportWrapper('picorb_create_task_with_filename', 2);
@@ -5866,6 +5877,8 @@ var wasmImports = {
5866
5877
  /** @export */
5867
5878
  js_clear_timeout,
5868
5879
  /** @export */
5880
+ js_copy_last_error,
5881
+ /** @export */
5869
5882
  js_create_array,
5870
5883
  /** @export */
5871
5884
  js_create_callback_function,
@@ -5886,6 +5899,8 @@ var wasmImports = {
5886
5899
  /** @export */
5887
5900
  js_inspect_to_buffer,
5888
5901
  /** @export */
5902
+ js_last_error_length,
5903
+ /** @export */
5889
5904
  js_register_generic_callback,
5890
5905
  /** @export */
5891
5906
  js_remove_attribute,
@@ -5934,7 +5949,7 @@ var wasmImports = {
5934
5949
  /** @export */
5935
5950
  serial_binary_capture_stop,
5936
5951
  /** @export */
5937
- serial_capture_get,
5952
+ serial_capture_copy,
5938
5953
  /** @export */
5939
5954
  serial_capture_start,
5940
5955
  /** @export */
@@ -47,7 +47,15 @@
47
47
  Module.picorubyRun = function() {
48
48
  const MRB_TICK_UNIT = 4; // Must match the value in build_config/picoruby-wasm.rb
49
49
  const BATCH_DURATION = 16; // ~1 frame (16.67ms)
50
+ const IDLE_DELAY = 4;
50
51
  const MAX_CATCHUP_TICKS = 10; // Cap to avoid freeze when tab returns from background
52
+ const runStepStatus = Module._mrb_run_step_status || function() {
53
+ const result = Module._mrb_run_step();
54
+ return result < 0 ? -1 : 1;
55
+ };
56
+ const gcSchedulerPending = Module._mrb_gc_scheduler_pending_wasm || function() {
57
+ return 0;
58
+ };
51
59
  let lastTick = performance.now();
52
60
  function run() {
53
61
  const now = performance.now();
@@ -61,13 +69,20 @@
61
69
  lastTick = now;
62
70
  }
63
71
  const sliceStart = performance.now();
72
+ let progressed = false;
64
73
  while (performance.now() - sliceStart < BATCH_DURATION) {
65
- const result = Module._mrb_run_step();
66
- if (result < 0) {
67
- console.error('mrb_run_step returned', result, '- scheduler continues');
74
+ const status = runStepStatus();
75
+ if (status < 0) {
76
+ console.error('mrb_run_step_status returned', status, '- scheduler continues');
77
+ break;
68
78
  }
79
+ if (status === 0) {
80
+ break;
81
+ }
82
+ progressed = true;
69
83
  }
70
- setTimeout(run, 0);
84
+ const delay = progressed || gcSchedulerPending() === 1 ? 0 : IDLE_DELAY;
85
+ setTimeout(run, delay);
71
86
  }
72
87
  run();
73
88
  };