Recent Changes - Search:

edit SideBar

DuktapeHost

The Duktape Host is a work in progress

"Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint."

Status

The Duktape host is an effort to run accessors in a very small memory footprint.

The executable is under 300k.

Under Mac OS X, using the Activity Monitor to inspect the duk process shows:

  • Real Memory Size: 1.5MB
  • Virtual Memory Size: 2.38GB
  • Shared Memory Size: 224 KB
  • Private Memory Size: 848 KB.

To get this data, I added a busy loop at the end of test/testComposite.js:

function sleepFor( sleepDuration ){
    var now = new Date().getTime();
    while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}

sleepFor(20000);

For hints about lowering the memory usage see

The current status is that accessors can be instantiated and run, but there is much to do.

See Maxim Pegasus for running on a 512K RAM/2Mb Flash Cortex-M4.

A Simple Example

In the example below, we compile the duk executable from sources that include a C function that reads files.

We then run duk on a file that uses test/testComposite.js

Due to limitations about how Duktape handles relative paths in require(), we need to run this from the hosts/ directory, not the hosts/duktape/ directory.

Note that the results is 50!

bash-3.2$ cd accessors/web/hosts/duktape/duktape
bash-3.2$ make clean
rm -f duk
bash-3.2$ make
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile.cmdline
gcc -o duk  -Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer -I. -I./src    \
-DDUK_OPT_SELF_TESTS src/duktape.c examples/cmdline/duk_cmdline.c examples/eventloop/fileio.c e\
xamples/eventloop/poll.c -lm
bash-3.2$ ls -l duk
-rwxr-xr-x  1 cxh  staff  300376 Apr 26 14:21 duk
bash-3.2$ cd ../..
bash-3.2$ ./duktape/duktape/duk --accessor --timeout 2000 duktape/test/testComposite.js
duktapeHost.js done
duk: About to instantiate duktape/test/testComposite.js
timeout 2000
../eduk/nofileio.c:163 Could not find ./duktape/test/testComposite.js
../eduk/nofileio.c:163 Could not find ../duktape/test/testComposite.js
../eduk/nofileio.c:163 Could not find ../../duktape/test/testComposite.js
TestGain: input 10 gain: 4
TestAdder: inputLeft: 10 inputRight: 40
duktape/test/testComposite.js: OK: output was 50, which is equal to 50.
bash-3.2$
 

Invoke Composite Accessor

duk_cmdline.c has been extended to handle

--accessor
Run the command line arguments as Accessors
--timeout time
Exit after time milliseconds

See also Command Line Arguments.

For example:

  1. Use the Cape Code Host to generate a composite accessor
    1. Invoke vergil:
      $PTII/bin/vergil /Users/cxh/ptII/ptolemy/cg/kernel/generic/accessor/test/auto/RampJSDisplay.xml
    2. Click on the AccessorCodeGenerator and then the Generate button
    3. hosts/node/RampJSDisplay.js will be created note: this file may be moved sometime soon.
  2. Invoke the Duktape host:
    1. Build:
      cd hosts
      svn update
      (cd duktape/duktape; make clean; make)
    2. Invoke the Duktape Accessor Host:
      bash-3.2$ duktape/duktape/duk --timeout 5000 --accessor node/RampJSDisplay.js
      examples/cmdline/duk_cmdline.c Loading C version of duktapeHost
      loading module: common/commonHost
      loading module: common/commonHost.js
      loaded Ecmascript: common/commonHost.js
      loading module: common/modules/util.js
      loading module: common/modules/util.js
      loaded Ecmascript: common/modules/util.js
      loading module: common/modules/events.js
      loading module: common/modules/events.js
      loaded Ecmascript: common/modules/events.js
      loading module: duktape/duktape/examples/eventloop/ecma_eventloop
      loading module: duktape/duktape/examples/eventloop/ecma_eventloop.js
      loaded Ecmascript: duktape/duktape/examples/eventloop/ecma_eventloop.js
      Loaded duktapeHost.js
      examples/cmdline/duk_cmdline.c: Loading C version of duktapeHost worked
      duktapeHost.js: instantiateAndInitialize() start: node/RampJSDisplay.js 1
      duktapeHost.js: instantiateAndInitialize(): node/RampJSDisplay.js
      Instantiated accessor RampJSDisplay.js with class node/RampJSDisplay.js
      ecmap_eventloop.js: setInterval(): 1000
      ecmap_eventloop.js: setTimeout(): 5000
      calling EventLoop.run()
      ecmap_eventloop.js: setTimeout(): 0
      ecmap_eventloop.js: setTimeout(): 0
      1
      ecmap_eventloop.js: setTimeout(): 0
      ecmap_eventloop.js: setTimeout(): 0
      2
      ecmap_eventloop.js: setTimeout(): 0
      ecmap_eventloop.js: setTimeout(): 0
      3
      ecmap_eventloop.js: setTimeout(): 0
      ecmap_eventloop.js: setTimeout(): 0
      4
      bash-3.2$

Debugging

Compile with Debugging

Adding the following to duktape.c and recompiling turns on debugging:

#define DUK_USE_DEBUG 1
#define DUK_USE_DDPRINT 1

JSON.Stringify

To print the contents of an object in Duktape, download json2.js

  cd hosts/duktape                                                                          
  wget https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js           

And then use code like:

var json = require("duktape/json2");                                                        
console.log("commonHost is: " + JSON.stringify(commonHost));  

See Also

Edit - History - Print - Recent Changes - Search
Page last modified on April 14, 2017, at 05:58 am