Skip to content

Conversation

@kaspar030
Copy link
Contributor

@kaspar030 kaspar030 commented Oct 23, 2017

This pull requests adds the foundation for accessing RIOT's API through javascript.

Currently available are timers and saul.

It uses the event system introduced in #6000 in order to asynchronously pass e.g., interrupts into javascript (whose state cannot be modified while it is running).

This is WIP as we need to agree on the concept, and licenses and documentation still missing.
Waiting for #7794, #6000.

@kaspar030 kaspar030 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR State: waiting for other PR State: The PR requires another PR to be merged first State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet labels Oct 23, 2017
@kaspar030
Copy link
Contributor Author

needs #7794, #6000

@kaspar030
Copy link
Contributor Author

needs #6000

@kaspar030 kaspar030 removed the State: waiting for other PR State: The PR requires another PR to be merged first label Nov 7, 2017
@@ -1 +1,12 @@
print("Hello from JerryScript!");
print("Hello from JerryScript!!!!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real important change ^^!!!!

@miri64
Copy link
Member

miri64 commented Nov 14, 2017

Let me know if this isn't WIP anymore.

@kaspar030
Copy link
Contributor Author

Let me know if this isn't WIP anymore.

Actually, this is more "Request for comments". I don't know enough javascript to say if the API makes sense (from a javascript conventions perspective)...

@danpetry danpetry mentioned this pull request Jan 25, 2018

js_xtimer_t *js_xtimer = (js_xtimer_t *) native_p;
xtimer_remove(&js_xtimer->xtimer);
free(js_xtimer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaspar030 isn't jerryscript freeing this js_xtimer block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, jerryscript garbage-collects the javascript timer object. If that happens to be a "native" object, it calls that native object's free_cb, which happens to be this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I see. But why is it required to call free()? Shouldn't the js memory manager free the whole block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, when the native timer object is created (using js_object_native_create()), the js_xtimer_t is malloc'ed outside jerryscript's heap (so we still have control over it). Thus our binding code both allocateds and frees the object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I see ;)

@jia200x
Copy link
Member

jia200x commented Feb 6, 2018

Very nice! I like it.
Just some minor comments:

  1. Do you think writing lib.js functions directly in RIOT (and map them as js functions) could help to decrease memory consumption? Could also help to reuse code in case Lua or other scripting languages get into mainstream
  2. Do you think it's possible to use TLSF instead of jerryscript heap implementation? So all processes can share the same heap.

On the other side, at some point could be nice to have a tiny RIOT-JS framework focused in IoT scenarios. But for now, I think the idea is ok and would be nice to have it merged. After that, it's easier to work in the a better API.

@kaspar030
Copy link
Contributor Author

Thanks a lot for the feedback!

Do you think writing lib.js functions directly in RIOT (and map them as js functions) could help to decrease memory consumption? Could also help to reuse code in case Lua or other scripting languages get into mainstream

Sure! It was just very convenient to write lib.js in Javascript. :)
It might not be worth the hassle for some things, but I guess we'll have to find out.

Do you think it's possible to use TLSF instead of jerryscript heap implementation? So all processes can share the same heap.

Difficult. Jerryscript uses non-trivial pointer compression in order to keep memory usage down, so this is not a matter of just mapping "js_malloc" to "tlsf_malloc", and might turn out counter-productive.

(TLSF also eats >3kB just for house keeping in its default configuration).

@jia200x
Copy link
Member

jia200x commented Feb 7, 2018

Sure! It was just very convenient to write lib.js in Javascript. :) It might not be worth the hassle for some things, but I guess we'll have to find out.

I will give it a try and let you know ;)

Difficult. Jerryscript uses non-trivial pointer compression in order to keep memory usage down, so this is not a matter of just mapping "js_malloc" to "tlsf_malloc", and might turn out counter-productive.

I see

@jia200x
Copy link
Member

jia200x commented Feb 7, 2018

@kaspar030 if you like the idea in #8533, there could be a make js command at some point :)

@jia200x
Copy link
Member

jia200x commented Mar 20, 2018

just noticed something weird in native:

Executing lib.js...
Hello from lib.js
Executing main.js...
Hello from JerryScript!!!!
js_run(): Script Error!
Entering event loop...
^C
native: exiting

There's is a Script Error here.
Same code on samr21-xpro:

2018-03-20 16:16:52,922 - INFO # main(): This is RIOT! (Version: 2018.01-devel-280-gdf8da-archlinux-riot.js)
2018-03-20 16:16:52,926 - INFO # You are running RIOT on a(n) samr21-xpro board.
2018-03-20 16:16:52,929 - INFO # This board features a(n) samd21 MCU.
2018-03-20 16:16:52,932 - INFO # Initializing jerryscript...
2018-03-20 16:16:52,935 - INFO # Executing lib.js...
2018-03-20 16:16:52,965 - INFO # Hello from lib.js
2018-03-20 16:16:52,968 - INFO # Executing main.js...
2018-03-20 16:16:52,975 - INFO # Hello from JerryScript!!!!
2018-03-20 16:16:52,981 - INFO # Entering event loop...

@jia200x
Copy link
Member

jia200x commented Mar 21, 2018

@kaspar030 have you checked JERRY_SYSTEM_ALLOCATOR? It forces js to use system's malloc, as seen here


static void js_callback_cleanup(js_callback_t *js_callback)
{
DEBUG("release %lu\n", callback);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sed 's/callback/js_callback'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and add ->callback.object?)

@emmanuelsearch
Copy link
Member

what is WIP here?
what are steps towards having some version of this in master?

@danpetry danpetry assigned danpetry and unassigned danpetry May 28, 2018
@tcschmidt
Copy link
Member

@emmanuelsearch It looks like there are changes requested. Needs resolution, I suppose.


led.write(1);

button.on_threshold(0, function(){led.write(0); return true;}, saul.op.EQ);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaspar030 how about showing some timer function too, for documentation purposes ;)
Something more or less like below, i.e. blink if you press the button?

blink = function () {
while (1) {
    value += 1 % 2;
    this.led.write(value);
    t = timer.setTimeout(this.blink, 500000);
   }
}
button.on_threshold(0, this.blink}, saul.op.EQ);

@jia200x
Copy link
Member

jia200x commented Sep 26, 2018

@kaspar030 may I open a rework of the event handler part, and then gradually add the remaining parts of the lib.js file?

@jia200x
Copy link
Member

jia200x commented Oct 4, 2018

@kaspar030 may I open a rework of the event handler part, and then gradually add the remaining parts of the lib.js file?

ping @kaspar030

@kaspar030
Copy link
Contributor Author

@kaspar030 may I open a rework of the event handler part

Sure. What's wrong with this?

@jia200x
Copy link
Member

jia200x commented Oct 4, 2018

Sure. What's wrong with this?

Nothing wrong. Just add Doxygen labels, patch pointed out errors and reduce the scope to speed up the merge. What do you think?

@stale
Copy link

stale bot commented Aug 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@kaspar030 kaspar030 added the State: don't stale State: Tell state-bot to ignore this issue label Aug 19, 2019
@stale stale bot removed the State: stale State: The issue / PR has no activity for >185 days label Aug 19, 2019
@miri64
Copy link
Member

miri64 commented Jun 23, 2020

@kaspar030 any plans to work on this in the near future?

@MrKevinWeiss MrKevinWeiss added this to the Release 2021.07 milestone Jun 21, 2021
@MrKevinWeiss MrKevinWeiss removed this from the Release 2021.07 milestone Jul 15, 2021
@Teufelchen1
Copy link
Contributor

@kaspar030 👀

@kaspar030
Copy link
Contributor Author

I won't work on this anymore.

@kaspar030 kaspar030 closed this Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR State: don't stale State: Tell state-bot to ignore this issue State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants