Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 22, 2017, 05:22 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Duktape/Dukglue: Embed JavaScript in your application without blowing it up!  (Read 254 times)

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,897
    • View Profile
    • Donate to Member
While reviewing small browsers, I came across NetSurf as well. NetSurf is a fast and bloat-free web browser which happens to have got JavaScript support in younger incarnations by adding the Duktape ECMAScript engine.

Duktape, written in C, is much smaller than V8 or similar JS engines, making it a good choice for embedded systems or things like adding a scripting functionality to existing software. No web browser involved! It supports ECMAScript 5.1 with some additions from newer revisions, including regular expressions and Unicode. For lazy people like me, there is a C++ wrapper named Dukglue (quack!) available which lacks (good) documentation but works like a charm. If you ever thought that your application could need a good scripting engine, Duktape seems to be a good choice. Your C/C++ code can access any script functions and your scripts can access exported C/C++ symbols which is easier than it sounds.

You want an example? You get an example!
Assume that you want to have a JavaScript plug-in which doubles any input value.

Code: Javascript [Select]
  1. var DemoScript = {};
  2. DemoScript.double = function(number) {
  3.     return number*2;
  4. }
  5. DemoScript;

Here's how Dukglue probably* handles it:

Code: C++ [Select]
  1. #include <string>
  2. #include <stringstream>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <dukglue/dukglue.h> // <- this is important
  6.  
  7. // ... blah ...
  8.  
  9. void processScripts(scriptfile) {
  10.     duk_context* ctx = duk_create_heap_default();
  11.    
  12.     std::ifstream infile(scriptfile); // add the exists() check yourself pls.
  13.     std::string file_contents = [&infile] {
  14.         // Read the script file contents for processing.
  15.         std::ostringstream ss{};
  16.         ss << infile.rdbuf();
  17.         return ss.str();
  18.     }();
  19.  
  20.     try {
  21.         // Give me a local handle to the script:
  22.         DukValue script = dukglue_peval<DukValue>(ctx, file_contents.c_str());
  23.  
  24.         // Try to call double(2):
  25.         int value = dukglue_pcall_method<int>(ctx, script, "double", 2);
  26.         std::cout << "The double value of 2 is " << value << std::endl;
  27.     }
  28.     catch (DukException& e) {
  29.         // Damn.
  30.         std::cout << "Blimey! Something bad happened. " << e.what();
  31.     }
  32.    
  33.     duk_destroy_heap(ctx);
  34. }
  35.  
  36. // now call processScripts("double.js"); or whatever.

* I was lazy, I just assumed it would work like this and it probably does. A more complex and actually tested ;) example is part of my blog software (nudge, nudge).

According to mouser, this might be interesting for more people than just me. So here it is. :)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,418
    • View Profile
    • Donate to Member
Neat. That could be useful for us c++ coders. Thanks for sharing that  :up: