espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.73k stars 741 forks source link

Esp32 cleanup jswarn #2478

Closed MaBecker closed 2 months ago

MaBecker commented 3 months ago

for details see comments with first commit

gfwilliams commented 3 months ago

Otherwise this looks good though - it'll be nice to avoid those annoying messages

MaBecker commented 3 months ago

Thanks for checking

MaBecker commented 3 months ago

Please do not merge yet, will let you when ready to merge

MaBecker commented 3 months ago

@gfwilliams can you please share how to handle data for callback functions.

Is this how to?

  JsVar *tmp = jsvNewObject();

  // Schedule callback if a function was provided
  jsvObjectSetChildAndUnLock(tmp, "hostname", jsvNewFromString(hostname));
  if (jsvIsFunction(jsCallback)) {
    JsVar *params[2];
    params[0] = jsvNewWithFlags(JSV_NULL);
    params[0] = tmp;
    jsiQueueEvents(NULL, jsCallback, params, 1);
    jsvUnLock(params[0]);
  }
gfwilliams commented 3 months ago

Yes, that looks good - but some typos in there....

MaBecker commented 3 months ago

I have no clue what

params[0] = jsvNewWithFlags(JSV_NULL);

is for

gfwilliams commented 3 months ago

Did that code come from somewhere? All that line does is make the first argument null - we were trying to keep the API the same between different ways to connect Espruino to the net, and also to keep it with a Node.js-ish API.

In some Node.js APIs they have a callback with an error argument. It's null of there's no error, or a String if there is an error: https://github.com/espruino/Espruino/blob/8f3a9cb52/libs/network/wiznet/jswrap_wiznet.c#L325

MaBecker commented 3 months ago

Yes, that looks good - but some typos in there....

  • the second params[0] should be [1]?
  • jsiQueueEvents last argument should be 2 because 2 arguments
  • tmp should be unlocked at the end of the function in the scope it was allocated in

like this?

JsVar *tmp = jsvNewObject();

  // Schedule callback if a function was provided
  jsvObjectSetChildAndUnLock(tmp, "hostname", jsvNewFromString(hostname));
  if (jsvIsFunction(jsCallback)) {
    JsVar *params[2];
    params[0] = jsvNewWithFlags(JSV_NULL);
    params[1] = tmp;
    jsiQueueEvents(NULL, jsCallback, params, 2);
    jsvUnLock(params[1]);
  }
  jsvUnLock(tmp);
gfwilliams commented 3 months ago

Nope - you changed jsvUnLock(params[0]); to jsvUnLock(params[1]); so now you're not unlocking the first arg but are unlocking the second one twice.

This should be fine:

JsVar *tmp = jsvNewObject();

  // Schedule callback if a function was provided
  jsvObjectSetChildAndUnLock(tmp, "hostname", jsvNewFromString(hostname));
  if (jsvIsFunction(jsCallback)) {
    JsVar *params[2];
    params[0] = jsvNewWithFlags(JSV_NULL);
    params[1] = tmp;
    jsiQueueEvents(NULL, jsCallback, params, 2);
    jsvUnLock(params[0]);
  }
  jsvUnLock(tmp);
gfwilliams commented 2 months ago

So do you think this is all ready now?

gfwilliams commented 2 months ago

Great - thanks!

MaBecker commented 2 months ago

Thanks for checking, tried to fix your findings. Would say good to go for now.