duktape

wrapper for the Duktape embeddable Javascript engine

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-21 05:24

Installation

nimble install duktape
choosenim install duktape
git clone https://github.com/manguluka/duktape-nim

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
duktape - - - - - - -

Source

Repository https://github.com/manguluka/duktape-nim
Homepage https://github.com/manguluka/duktape-nim
Registry Source nimble_official

README

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.