Spherical forums

Creations => Programming => Topic started by: N E O on December 06, 2014, 01:05:49 pm

Title: Duktape - embeddable-in-C/C++ JS engine
Post by: N E O on December 06, 2014, 01:05:49 pm
http://duktape.org/ (http://duktape.org/) (MIT licensed)

Looks about as simple, if not simpler, than SpiderMonkey. Anyone here want to benchmark speed vs. SpiderMonkey/Sphere, V8/TurboSphere, and/or Jurassic/SSFML?

I'll be playing around with Duktape a bit and see what I can get out of it, but until we know more I wouldn't knee-jerk immediately recommend we switch out SM, V8, or Jurassic just yet.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Radnen on December 06, 2014, 01:30:18 pm
I... like this Duktape library... I found this really an easy method to go between native and non-native execution:

Code: (cpp) [Select]

static duk_ret_t native_prime_check(duk_context *ctx) {
    int val = duk_require_int(ctx, 0); // get JS arg 0, make sure it's an int or throw error
    int lim = duk_require_int(ctx, 1); // get JS arg 1, make sure it's an int or throw error
    int i;

    for (i = 2; i <= lim; i++) { // native C++ code here
        if (val % i == 0) {
            duk_push_false(ctx); // return false in JS environment;
            return 1;
        }
    }

    duk_push_true(ctx); // return true in JS environment;
    return 1; // return success to C++ environment, (-1 = throw a failure).
}
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on December 11, 2014, 01:37:01 pm
Wow, that is nice.  It's been ages since I've seen such an elegant API for a C/C++ script library, reminds me a lot of Lua's.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on January 24, 2015, 11:57:37 am
So I've been playing with this along with SFML in VC++, and it's indeed pretty nice.  The main issue here is duktape being a C library, it doesn't really cooperate with C++ well.  I also haven't done any benchmarks to ascertain it's performance; but I can't imagine it's much faster than the Spidermonkey in Sphere 1.5.  It seems far too lightweight for any JITting to be going on here.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Flying Jester on January 24, 2015, 06:36:18 pm
Some C libraries are fine to use in C++...but looking at this one, it reminds me a lot of embedding Python. Which is not really a good thing.

I will keep this library in mind for future projects. There have been times when I wanted to quickly prototype some algorithm or calculation, and easily writing just a little JS to do that sounds appealing.

Just thinking about what I imagine this library would be useful for...

If I was going to make the simplest possible Sphere-like program, I might consider using this, and exposing an absolutely tiny native API and writing the rest in script. Speed might be an issue, but there is the certain appeal of making it the simplest possible.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Radnen on January 24, 2015, 09:57:02 pm

If I was going to make the simplest possible Sphere-like program, I might consider using this, and exposing an absolutely tiny native API and writing the rest in script. Speed might be an issue, but there is the certain appeal of making it the simplest possible.


Yes, definitely a MiniSphere. I wonder then, if it's possible to completely make the most cross-platform Sphere engine using Allegro and Duktape, so that it could run on as many devices as possible. In essence it may not be lightning fast but you are giving up performance for portability and simplicity.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Flying Jester on January 25, 2015, 12:33:25 am

Yes, definitely a MiniSphere. I wonder then, if it's possible to completely make the most cross-platform Sphere engine using Allegro and Duktape, so that it could run on as many devices as possible. In essence it may not be lightning fast but you are giving up performance for portability and simplicity.


Expose the screen as an ArrayBuffer, and put all drawing in script, too! :P
You'd probably hit very serious performance issues with any JS engine doing that. I was thinking that surfaces could be totally in script, though. Just have the Image API, and add a `CreateImage(pixels, width, height);' function.

The beauty of having such a thing would be that, for one, it would basically run absolutely anywhere, and two any other engine could use a portion of a NanoSphere's scripts to fill in functionality that is either not implemented in native, or just not yet implemented in native.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on January 25, 2015, 01:31:13 am
See, this is what I was thinking as well, use this as a springboard to make a cross-platform minimal Sphere engine in C or C++.  Except Allegro wasn't my first choice, it was actually SFML.  Radnen has already proved SFML can work for this purpose, but it would be even more awesome if it didn't require .NET.  The thing is though, how would Duktape hold up performance-wise against Jurassic?
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Radnen on January 25, 2015, 01:40:31 am
One thing I know is... Jurassic is definitely not portable - I've tried compiling it under Mono, (Mono just came out with it's latest version for Windows so I'll try again). Second, Jurassic is C# and not C/C++ and I want this to run on anything.

Allegro, unlike SFML is proven to work on the 5 major brands: http://alleg.sourceforge.net/readme.html (SFML will soon, though, the support is there but I still think there hasn't been an official release).

Allegro is natively C (and C++) and so is best choice for that language. So is SFML but it isn't as cross-platform and I think Allegro is lighter weight? I'm not sure if allegro is necessarily faster, but it's API is fairly easy to figure out too.

Jurassic is leagues faster than Duktape (but, both support ECMA 5.1 standard). Duktape does say it has some ECMA6 stuff in it, so here's hoping for TypedArray. Just to have an idea, Jurassic is between Sphere 1.5 and any V8/SM Sphere in performance, so it's not the fastest but it can be easily 32 times faster, whereas V8 has been 58 times faster on some things (I have tested this using an internal build of SSFML using ClearScript (a currently maintained V8 wrapper for C#)).

Plus, anything C or C++ can be compiled into almost anything these days. C# and Java can only go where their VM's go and C#'s is limited to Windows only or anything Mono would try to support (which is Android and iPhone too). But, SFML and Jurassic would be severely limited on those two devices, which a native C app one of which was built with Allegro could potentially run faster and smoother on those devices. Plus, memory is a concern and by far C has the smallest footprints ever recorded. Git is programmed entirely in C and I'm always a fan of Linus's work (though I don't often use Linux myself, I have always loved it's simplicity as a programming environment, it's how I got through my CS major!)

I +1 any idea going forward in this manner.

Edit: Heh, I love XNA and Allegro and found out both projects were led by Shawn Hargreaves (well, Allegro started by him, and XNA ending with him). Wow. (Though SFML is good too, :)))
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on January 25, 2015, 02:06:54 am
I don't know, if Duktape doesn't have the performance behind it then I can't see this as a worthwhile venture in light of TurboSphere's existence.  No need to fragment Sphere engine development any further when TS will likewise (at least in theory...) be cross-platform as well, being written in C++ itself.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Radnen on January 25, 2015, 02:47:25 am
Well I say Duktape since it tries to be very portable, but if SM proves to do quite well, then that's good too.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Flying Jester on January 25, 2015, 03:04:43 am
though I don't often use Linux myself, I have always loved it's simplicity as a programming environment, it's how I got through my CS major!


Have you tried FreeBSD? I eventually became sick of even Linux's complexity (systemd primarily), and that's where I ended up.


I don't know, if Duktape doesn't have the performance behind it then I can't see this as a worthwhile venture in light of TurboSphere's existence.  No need to fragment Sphere engine development any further when TS will likewise (at least in theory...) be cross-platform as well, being written in C++ itself.


TS needs platform maintainers to truly exist as a cross-platform engine, just as any other engine likely will. It's proven to be portable between OS's and compilers, but with only a single maintainer, it is really only expected to run on a single platform on any one release.

I for one would certainly support a NanoSphere project, and I would try my best to share as much script-side engine code as possible with it. That, I think, would be the biggest strength. I would love to see such a reference implementation exist, and I think it really would be worthwhile as a jumping off point for a lot of new features. Done well, any other engine could also contribute back to it in some meaningful way.

It's a good point, though, that SM is also extremely portable.
The only thing that it properly needs ported to run is NSPR, but that works anywhere pthreads is available (so everywhere I know of).
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on January 25, 2015, 10:47:19 am
Well, I guess I'll go ahead with this then.  Here's my plan: Make a small reference implementation of Sphere using Duktape and SFML in C (not C++).  I can't promise blazing performance, but it shouldn't really be any slower than Sphere 1.5 in any case, and it'll have the advantage of being a modern codebase.

I'm tentatively dubbing it minisphere.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on April 10, 2015, 03:29:23 am
^^^^^ History in the making, folks! ^^^^^ :P ;D

But seriously, yes, in case there was any doubt after how successful minisphere was, Duktape is awesome.  Even more awesome: the Duktape developer.  Nine times out of ten, I've reported a bug or deficiency in the engine, it's been fixed literally within hours.  For anyone wanting to use JS in their app with minimal effort, I can't recommend this enough. 8)
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on December 09, 2015, 11:49:12 pm

Yes, definitely a MiniSphere. I wonder then, if it's possible to completely make the most cross-platform Sphere engine using Allegro and Duktape, so that it could run on as many devices as possible. In essence it may not be lightning fast but you are giving up performance for portability and simplicity.


Radnen, are you clairvoyant? :P
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Flying Jester on December 10, 2015, 01:08:54 am
To be fair, both Sphere 1.5 and TurboSphere (at various points) have so far been compiled and run on a wider variety of platforms that MiniSphere has yet.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on December 10, 2015, 01:44:45 am
No, I wasn't referring to that, I was specifically pointing out Radnen's mention of Allegro, I think about a month before I actually considered using it (minisphere started out as an SFML engine, the day after that post).
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Radnen on December 10, 2015, 08:48:52 pm
I always assumed you got the idea from me. :P It's what I would have done... obviously now that I've re-read the comment. lol It was a solid statement, but you might be able to get more performance out of sfml? Anyways it really largely depends on duktape getting faster for you.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Flying Jester on December 10, 2015, 08:58:32 pm
Does nobody love SDL2? ;_;
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on December 10, 2015, 10:17:26 pm
Nothing specifically against it, it just happened that I tried Allegro first (after being put off by SFML's over-complex "object-oriented-for-the-sake-of-it" API) and fell in love with its API, so I stuck with it.  I also wasn't that big a fan of SDL1 when I tried it a few years ago, but I don't know how different 2.0 is.

I like that they went with a zlib license for SDL2 though, it used to be GPL (I think?).  Not really big on copyleft licenses.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Radnen on December 10, 2015, 11:14:25 pm

(after being put off by SFML's over-complex "object-oriented-for-the-sake-of-it" API)


This is why I like using SFML with .NET more than anything.
Title: Re: Duktape - embeddable-in-C/C++ JS engine
Post by: Fat Cerberus on September 07, 2017, 06:39:22 pm
If there's ever a Sphere museum, this topic belongs in it. :D

Who would have guessed that NEO posting offhandedly about a (at the time) brand new JS engine would kickstart the development of the first successful (and epically so!) Sphere v2 engine?