Skip to content
Nim wrapper for the Duktape embeddable Javascript engine.
C C++ Nim
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
duktape/src
src
tests
.gitignore
README.md update readme usage example Jul 26, 2018
duktape.cfg
duktape.nimble

README.md

Duktape-nim is a Nim wrapper for the Duktape embeddable Javascript engine.

Installation

Use Nimble:

> nimble install duktape

> git clone https://github.com/manguluka/duktape-nim
> cd duktape-nim
> nimble install -y

Usage

Interpret Javascript expression

import duktape/js

# Create duktape context
var ctx = duk_create_heap_default()

# interpret Javascript expression
ctx.duk_eval_string( "1+20");
# Retreive value of last expression as an int
assert ctx.duk_get_int(-1) == 21

# interpret Javascript expression
ctx.duk_eval_string( "'foo'+'bar'+1");
# Retreive value of last expression as a string
assert ctx.duk_get_string(-1) == "foobar1"

# Cleanup duktape memory
ctx.duk_destroy_heap();

Bind Nim proc and call from Javascript

import duktape/js

# Define proc to be bound
var println: DTCFunction = (proc (ctx: DTContext): cint{.stdcall.} =
    echo duk_to_string(ctx, 0)
)

# Create duktape context
var ctx = duk_create_heap_default()

# Bind proc to the context and global "println" variable 
var err = ctx.duk_push_c_function( println, cast[cint](-1))
var r = ctx.duk_put_global_string("println")

# Call bound proc from Javascript
ctx.duk_eval_string( "println('foobar'+2)");

# Cleanup duktape memory
ctx.duk_destroy_heap();

Credits

Duktape-nim depends on nimgen and c2nim.

You can’t perform that action at this time.