Skip to main content

News

Topic: Duktape - embeddable-in-C/C++ JS engine (Read 18218 times) previous topic - next topic

0 Members and 2 Guests are viewing this topic.
  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Duktape - embeddable-in-C/C++ JS engine
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.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #1
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).
}
  • Last Edit: December 06, 2014, 01:33:08 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #2
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.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #3
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.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

Re: Duktape - embeddable-in-C/C++ JS engine
Reply #4
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.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #5

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.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Duktape - embeddable-in-C/C++ JS engine
Reply #6

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.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #7
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?
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #8
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, :)))
  • Last Edit: January 25, 2015, 02:03:23 am by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #9
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.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #10
Well I say Duktape since it tries to be very portable, but if SM proves to do quite well, then that's good too.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Duktape - embeddable-in-C/C++ JS engine
Reply #11
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).

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #12
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.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #13
^^^^^ 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)
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Duktape - embeddable-in-C/C++ JS engine
Reply #14

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
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub