Author Topic: Embedded JavaScript for extending C, mJS, takes 25K Flash  (Read 6054 times)

0 Members and 1 Guest are viewing this topic.

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Embedded JavaScript for extending C, mJS, takes 25K Flash
« on: March 25, 2017, 05:07:25 pm »
Found this recently! Not associated with them, but I'm excited to find something that might work for me. Looking for opinions.

Not sure what to think of it yet. Seems like it's PERFECT for a scripting project I have where I want to take in a remotely generated list of C functions and run them handle from script.

I was on the DSL path but handling logic like "wait for state to be true", "if else branch" and etc was going to be a lot of work. Plus just keeping up to date on the DSL device and remote side was going to be a challenge.

Looked at MicroPython and eLua from advice here, 128K and 192K MINIMUM respectively.

No std library, seems designed for embedded. Same guy who does "V7" JS interpreter. This seems to be a deconteded derivative with one addition I like. FFI, Foriegn Feature Interface.

In order to call a C function from JS it's:

Code: [Select]
let f = ffi('int gpio_write(int, int)'); 
f(2, 1);

25KB Flash, 1KB RAM, one C file one H file, both look easy enough to follow and have decent comments. It SEEMS like exactly what I was looking for. Not sure. Thoughts anyone?

https://mongoose-os.com/blog/mjs-a-new-approach-to-embedded-scripting/
https://github.com/cesanta/mjs
 
The following users thanked this post: evb149

Offline asgard20032

  • Regular Contributor
  • *
  • Posts: 184
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #1 on: March 25, 2017, 10:00:03 pm »
A 17k line of code file... NEVER. Its impossible to maintain such a big blob. He should have structured it in many file, easier to debug and maintain. How do you know if there ware bug in that big thing?
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #2 on: March 25, 2017, 11:23:02 pm »
A 17k line of code file... NEVER. Its impossible to maintain such a big blob. He should have structured it in many file, easier to debug and maintain. How do you know if there ware bug in that big thing?

Saw that too. I think it's amalgamated for convenience once you know your code works. You can break that apart with the same app they use to combine it.

Certainly not a deal breaker for me. But question... since it's compiled with your code, wouldn't you just see it as debugging? When you are processing JS code you'll see the debugger jump to their code, execute and return context.

If you mean how do you debug your JavaScript code while in C, that applies to all scripting languages regardless of their c/h makeup. No?
 

Offline jancumps

  • Supporter
  • ****
  • Posts: 1272
  • Country: be
  • New Low
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #3 on: March 26, 2017, 08:18:16 am »
A 17k line of code file... NEVER. Its impossible to maintain such a big blob. He should have structured it in many file, easier to debug and maintain. How do you know if there ware bug in that big thing?
It is amalgamated, yes. The developer documented that in the C header.
Nothing wrong with that.
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #4 on: March 26, 2017, 09:28:01 am »
That piece of software is amazing, works well and easy

 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #5 on: March 26, 2017, 10:10:58 am »
Any idea about security? How well can you isolate a user from doing harmfull things? A scripting language on a microcontroller should be a sandbox where a user can play but not break things.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #6 on: March 26, 2017, 01:02:58 pm »
SSL and Certificates  is supported, everything works very easily, Just encrypt everything
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #7 on: March 26, 2017, 01:41:16 pm »
You can't implement SSL in 25k but that is not what I meant: how to prevent a script from doing harmfull things? Buffer overflows?
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #8 on: March 26, 2017, 04:58:21 pm »
You can't implement SSL in 25k but that is not what I meant: how to prevent a script from doing harmfull things? Buffer overflows?

Right, and that's a good point. I'll argue that if you have any scripting language that you have the same issue. Right? It becomes a matter of sanatizing input just like form to SQL hacks.

So what do you do? Well, for one I'm writing the only scripts, so I'll just have to test everything I write. Two, end to end encryption and/or sign the scripts with a separate tool, this should keep anyone else from submitting JS scripts. Three, something like a syntax checker (JS Lint?) that checks for commands outside of an allowed set. Like if you were trying to address a pointer to a memory location outside of an allowed range you could put a stop to that, this would be tricky because of the range of commands you could come up with.

Even if you write your own DSL, there is a chance someone could abuse it. Doesn't seem unique to this particular scripting implementation.


Maybe it just seems easier here because the developer thought to put an easy an obvious way to call C functions from JS?
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #9 on: March 27, 2017, 08:27:27 am »
"You can't implement SSL in 25k "

Wrong !!!!  25K is only the Java wrapper to call C functions ESP32 supports SSL natively with the SDK and Cryptographic hardware acceleration:
– AES
– HASH (SHA-2) library
– RSA
– ECC
– Random Number Generator (RNG)



 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #10 on: March 27, 2017, 09:14:16 am »
So not wrong. You need several hundred K of library to compile against.
Quote
448 KB ROM for booting and core functions
Yeah.

Title should be:
"Embedded JavaScript for extending C, mJS, takes 473K Flash" for ESP32's

But the concept looks nice. But I think it won't stay small for long.

* :wtf: 15 thousand source lines of code*
« Last Edit: March 27, 2017, 09:19:32 am by Jeroen3 »
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #11 on: March 27, 2017, 09:29:25 am »
I'm sorry but you missed the point

That tool is amazing, and you are wrong again still only 25K, and that is the smallest and easiest path to mix C and javascript, Does not exist anything smaller with same functionality, At least try before  getting "smart"
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #12 on: March 27, 2017, 01:07:09 pm »
About the SSL? I think not.

The mJS is quite small. It does not seem to use any standard library. Someone has been very busy with it.

Although I don't think it can be maintained very well like this.Or am I missing some special tool?
 

Offline krho

  • Regular Contributor
  • *
  • Posts: 223
  • Country: si
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #13 on: March 27, 2017, 03:01:00 pm »
But before using this for anything that goes out of the lab. Please remember that you either have to release the source code with your product or. buy a commercial license.
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #14 on: March 27, 2017, 03:09:58 pm »
SSL discussion over. Not part of this conversation. It's the JS for C interpiter that is 25K that also seems to include a JSON parser.

As to someone being very busy with this source, it seems to me this is a direct port/modification of the V7 JS for C project reduced in size and features changed. It's also hard to find a lot of on-topic discussion regarding V7.

I've been going over the source and it seems decent. If I'm not pushing the limits of JS inside of C, I imagine it could be fairly reliable. I want to see what kind of overhead and unpredictability I can manage to find in some test examples but won't be able to make time for that for a few weeks :/
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #15 on: March 27, 2017, 07:53:05 pm »
I'm a complete noob when it comes to javascript so I hope somebody can shine some light on this. I recon regular javascript has some kind of standard library. Is this included as well? If not what would be some minimum requirement?
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline KE5FX

  • Super Contributor
  • ***
  • Posts: 1889
  • Country: us
    • KE5FX.COM
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #16 on: March 27, 2017, 08:08:20 pm »
GPL?  Really?  :-X
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #17 on: March 27, 2017, 11:58:19 pm »
I'm a complete noob when it comes to javascript so I hope somebody can shine some light on this. I recon regular javascript has some kind of standard library. Is this included as well? If not what would be some minimum requirement?

No standard library. If you wanted to extend it with text string handling you'd have to include/import that.

This seems to have logic, loops, math ops, and function calling. That's about it. Javascript's stdlib includes date formatting and etc, I have little use for that in a micro.

FOR ME, I don't care that it's javascript. I care that it's SCRIPTING that happens to use javascript syntax. In 25KB I can create a scripting instance, send in commands, call C functions from elsewhere in ROM from it, awesome. It's not like Espruino where C is entirely replaced with JS, in that case you would want a standard library. With this, you still have access to whatever C stdlib you are using.
« Last Edit: March 28, 2017, 12:03:37 am by jnz »
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #18 on: March 28, 2017, 04:12:13 pm »
GPL?  Really?  :-X

I know GPL is dying, but for me it doesn't apply (commercial license). But what is the issue with GPL in reality (not everyone is a lawyer fantasy world)?
 

Offline KE5FX

  • Super Contributor
  • ***
  • Posts: 1889
  • Country: us
    • KE5FX.COM
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #19 on: March 29, 2017, 12:11:56 am »
GPL?  Really?  :-X

I know GPL is dying, but for me it doesn't apply (commercial license). But what is the issue with GPL in reality (not everyone is a lawyer fantasy world)?

IMHO, and it's definitely just my opinion, it's lame to accept contributions under the GPL while dual-licensing commercially.  Why should I submit code to a GPL project where the maintainer gets paid for relicensing it under the table?

This sort of thing is trivial enough, but at the same time useful enough, that it should be both accessible and maintainable by any user.  Of course, it's easy to sit around telling other people what they should do for free...
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #20 on: March 30, 2017, 02:59:17 pm »
GPL?  Really?  :-X

I know GPL is dying, but for me it doesn't apply (commercial license). But what is the issue with GPL in reality (not everyone is a lawyer fantasy world)?

IMHO, and it's definitely just my opinion, it's lame to accept contributions under the GPL while dual-licensing commercially.  Why should I submit code to a GPL project where the maintainer gets paid for relicensing it under the table?

This sort of thing is trivial enough, but at the same time useful enough, that it should be both accessible and maintainable by any user.  Of course, it's easy to sit around telling other people what they should do for free...

Eh, I see your point. Gimme it doesn't matter because I'm just going to pay if I use it commercially and really have nothing to and no time to contribute anyhow.

But I get that asking people for help then using that license is a little dickish. I think if I were to guess as to why it's that this mJS code is what seems to drive one of their "real" products the MongooseOS in the video above. That's the whole IoT/SSL/TCP protocol OS and stack system.

So while I'm not using any of that, mJS should save me from having to do much of anything to get scripting a "Turing compete" system with very little overhead. Compare 25k to "micro"Python at 150k and it's not even a question. Plus without any direct stack work I can call C functions from script - I'm in love!
 
The following users thanked this post: rstofer

Offline Vtile

  • Super Contributor
  • ***
  • Posts: 1144
  • Country: fi
  • Ingineer
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #21 on: March 30, 2017, 03:28:25 pm »
AmForth seems to be 8K with whole development system on chip (Arduinos, MSP430). Might be worth to take a look for some hobbyist or obscure prof.project.
 

Offline jnzTopic starter

  • Frequent Contributor
  • **
  • Posts: 593
Re: Embedded JavaScript for extending C, mJS, takes 25K Flash
« Reply #22 on: March 31, 2017, 06:18:10 pm »
AmForth seems to be 8K with whole development system on chip (Arduinos, MSP430). Might be worth to take a look for some hobbyist or obscure prof.project.

Judging by your other thread... It's clear you are really into Forth right now, but go ahead and compare

let x=0;
let f = ffi('int myCfunc(int, int)'); 
let resp = f(10,20);
if (resp == 1)
x=1000;

To whatever that would be in Forth. I'd MUCH rather spend 25k and get something a entry level programmer could read or write to, over 8k for Forth and have something that almost no one can use or read quickly. Forth is manual stack management. If that's your thing, cool, but I think the world has moved on.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf