kaluma-project / kaluma

A tiny JavaScript runtime for RP2040 (Raspberry Pi Pico)
https://kalumajs.org
Apache License 2.0
645 stars 38 forks source link

System stuck when `.load` twice the example codes: https://kameleon.io/@niklauslee/ssd1306 #165

Closed niklauslee closed 5 years ago

niklauslee commented 5 years ago

Reproduce steps:

  1. upload ex_128x64.js (or ex_128x32.js) code in https://kameleon.io/@niklauslee/ssd1306. Upload action will load code automatically.
  2. enter .load in terminal.
  3. system stuck.

Same problem in https://kameleon.io/@niklauslee/ssd1351

niklauslee commented 5 years ago

This seems related with #3172 (I'm not sure)

niklauslee commented 5 years ago

Confirmed that jerry_cleanup() will call free callback functions. Need to check there is no reference from C API to the object having gc_handle.

niklauslee commented 5 years ago

More simplified bug reproduce code (Try in playground):

var BufferedGC = require('graphics').BufferedGraphicsContext;
var gc = new BufferedGC(128, 64, {
  display: (buffer) => {}
});
global.gc = gc;

The above code do not call free callback when .reset. But if removing display function, free callback successfully called when .reset

var BufferedGC = require('graphics').BufferedGraphicsContext;
var gc = new BufferedGC(128, 64, {
  // display: (buffer) => {}
});
global.gc = gc;
niklauslee commented 5 years ago

This issue is resolved. There were many reasons causing this problems:

  1. Try to release value of function argument especially in the places using JERRYXX_GET_ARG_OPT(). It shouldn't. Now JERRYXX_GET_ARG_OPT() has removed, so JERRYXX_GET_ARG() should be used with checking JERRYXX_HAS_ARG(). This problem were found in many native modules I2C, SPI, etc. Actually this problem caused system stuck.
  2. The callback functions in gc_handle struct (display_js_cb, etc.) prevented GC. So now move them into the class member variables (e.g. this.__display_cb) so as to allow GC.
  3. setTimeout() didn't release values even when it timed out. This also prevented GC.

Exact matching jerry_get/create_* and jerry_release_value is very important

communix commented 5 years ago

Great job!! Thank you for your hard work!