Convert Figma logo to code with AI

svaarala logoduktape

Duktape - embeddable Javascript engine with a focus on portability and compact footprint

5,921
514
5,921
462

Top Related Projects

Ultra-lightweight JavaScript engine for the Internet of Things.

8,251

Public repository of the QuickJS Javascript Engine.

1,422

Embedded JavaScript engine for C/C++

Quick Overview

Duktape is a lightweight, embeddable JavaScript engine that can be used as a scripting component in C programs. It is designed to be easy to integrate into existing applications and provides a small footprint, good performance, and a comprehensive set of features.

Pros

  • Lightweight and Embeddable: Duktape has a small footprint, making it suitable for embedded systems and other resource-constrained environments.
  • High Performance: Duktape is designed for good performance, with a focus on speed and efficiency.
  • Comprehensive Feature Set: Duktape supports a wide range of JavaScript features, including ES2015 (ES6) and beyond.
  • Cross-Platform: Duktape is designed to be cross-platform, with support for a variety of operating systems and architectures.

Cons

  • Limited Ecosystem: As a relatively niche project, Duktape may have a smaller ecosystem and community compared to more popular JavaScript engines like V8 or SpiderMonkey.
  • Fewer Optimizations: Duktape may not have the same level of optimization and performance tuning as larger, more widely-used JavaScript engines.
  • Fewer Bindings: The availability of pre-built bindings and integrations for Duktape may be more limited compared to other JavaScript engines.
  • Smaller Community: The Duktape project has a smaller community compared to more mainstream JavaScript engines, which may result in fewer resources and support available.

Code Examples

Here are a few examples of how to use Duktape in your C code:

// Evaluate a simple JavaScript expression
duk_context *ctx = duk_create_heap_default();
duk_eval_string(ctx, "1 + 2");
printf("Result: %d\n", (int)duk_get_int(ctx, -1));
duk_destroy_heap(ctx);
// Call a JavaScript function
duk_context *ctx = duk_create_heap_default();
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, "Math");
duk_get_prop_string(ctx, -1, "sqrt");
duk_push_number(ctx, 16.0);
duk_call(ctx, 1);
printf("Square root of 16: %f\n", duk_get_number(ctx, -1));
duk_pop_2(ctx);
duk_destroy_heap(ctx);
// Expose a C function to JavaScript
static duk_ret_t my_add(duk_context *ctx) {
    double a = duk_get_number(ctx, 0);
    double b = duk_get_number(ctx, 1);
    duk_push_number(ctx, a + b);
    return 1;
}

duk_context *ctx = duk_create_heap_default();
duk_push_c_function(ctx, my_add, 2);
duk_put_global_string(ctx, "add");
duk_eval_string(ctx, "print(add(40, 2))");
duk_destroy_heap(ctx);

Getting Started

To get started with Duktape, follow these steps:

  1. Download the latest version of Duktape from the GitHub repository.

  2. Extract the downloaded archive and navigate to the src-input directory.

  3. Compile the Duktape source files using your preferred C compiler. For example, with GCC:

    gcc -c duktape.c duk_module_node.c
    
  4. Link the compiled object files into your C program. For example:

    #include "duktape.h"
    
    int main() {
        duk_context *ctx = duk_create_heap_default();
    
        // Evaluate a simple JavaScript expression
        duk_eval_string(ctx, "1 + 2");
        printf("Result: %d\n", (int)duk_get_int(ctx, -1));
    
        duk_destroy_heap(ctx);
        return 0
    

Competitor Comparisons

Ultra-lightweight JavaScript engine for the Internet of Things.

Pros of JerryScript

  • Designed for resource-constrained devices, making it more suitable for IoT and embedded systems
  • Supports a wider range of architectures, including ARM, MIPS, and x86
  • Has a smaller memory footprint, typically requiring less than 64KB RAM

Cons of JerryScript

  • Less comprehensive ECMAScript support compared to Duktape
  • Fewer built-in features and extensions, which may require additional implementation
  • Smaller community and fewer third-party resources available

Code Comparison

JerryScript:

#include "jerryscript.h"

int main (void)
{
  const jerry_char_t script[] = "print('Hello, World!');";
  jerry_run_simple(script, sizeof(script) - 1, JERRY_INIT_EMPTY);
  return 0;
}

Duktape:

#include "duktape.h"

int main(int argc, char *argv[])
{
  duk_context *ctx = duk_create_heap_default();
  duk_eval_string(ctx, "print('Hello, World!');");
  duk_destroy_heap(ctx);
  return 0;
}

Both JerryScript and Duktape are lightweight JavaScript engines for embedded systems, but they have different strengths. JerryScript is more focused on resource-constrained devices, while Duktape offers more comprehensive ECMAScript support and additional features. The choice between them depends on the specific requirements of your project, such as memory constraints, target architecture, and needed JavaScript features.

8,251

Public repository of the QuickJS Javascript Engine.

Pros of QuickJS

  • Faster execution speed for many JavaScript operations
  • More complete implementation of ECMAScript 2020 features
  • Smaller memory footprint for certain use cases

Cons of QuickJS

  • Larger binary size compared to Duktape
  • Less extensive platform support, especially for embedded systems
  • Newer project with potentially less mature ecosystem

Code Comparison

QuickJS:

JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
const char *str = "console.log('Hello, World!');";
JS_Eval(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_GLOBAL);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);

Duktape:

duk_context *ctx = duk_create_heap_default();
const char *str = "print('Hello, World!');";
duk_eval_string(ctx, str);
duk_destroy_heap(ctx);

Both QuickJS and Duktape are lightweight JavaScript engines designed for embedding in C/C++ applications. QuickJS offers better performance and more modern JavaScript features, while Duktape excels in size and portability for embedded systems. The code comparison shows that QuickJS requires more setup but provides a more standardized API, whereas Duktape offers a simpler, more compact API for basic operations.

1,422

Embedded JavaScript engine for C/C++

Pros of v7

  • Smaller footprint and lower memory usage
  • Designed for embedded systems and IoT devices
  • Simpler API and easier integration

Cons of v7

  • Less feature-rich compared to Duktape
  • Smaller community and fewer resources available
  • Limited ECMAScript compliance

Code Comparison

v7:

struct v7 *v7 = v7_create();
v7_val_t result;
v7_exec(v7, &result, "2 + 2");
printf("Result: %d\n", v7_get_int(v7, result));
v7_destroy(v7);

Duktape:

duk_context *ctx = duk_create_heap_default();
duk_eval_string(ctx, "2 + 2");
printf("Result: %d\n", (int) duk_get_int(ctx, -1));
duk_destroy_heap(ctx);

Both v7 and Duktape are lightweight JavaScript engines designed for embedded systems. v7 focuses on minimalism and low resource usage, making it ideal for constrained environments. Duktape offers more features and better ECMAScript compliance but at the cost of a larger footprint. The code comparison shows that v7 has a slightly more verbose API, while Duktape's API is more concise. Choose v7 for extremely resource-limited devices, and Duktape for a more full-featured embedded JavaScript experience.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Duktape

Build status Test status

:warning: Master branch is undergoing incompatible changes for Duktape 3.x. To track Duktape 2.x, follow the v2-maintenance branch.

Introduction

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

Duktape is easy to integrate into a C/C++ project: add duktape.c, duktape.h, and duk_config.h to your build, and use the Duktape API to call ECMAScript functions from C code and vice versa.

Main features:

  • Embeddable, portable, compact
  • ECMAScript E5/E5.1 compliant, with some semantics updated from ES2015+
  • Partial support for ECMAScript 2015 (E6) and ECMAScript 2016 (E7), Post-ES5 feature status, kangax/compat-table
  • ES2015 TypedArray and Node.js Buffer bindings
  • WHATWG Encoding API living standard
  • Built-in debugger
  • Built-in regular expression engine
  • Built-in Unicode support
  • Minimal platform dependencies
  • Combined reference counting and mark-and-sweep garbage collection with finalization
  • Custom features like coroutines
  • Property virtualization using a subset of ECMAScript ES2015 Proxy object
  • Bytecode dump/load for caching compiled functions
  • Distributable includes an optional logging framework, CommonJS-based module loading implementations, CBOR bindings, etc
  • Liberal MIT license (see LICENSE.txt)

See duktape.org for packaged end-user downloads and documentation. The end user downloads are also available from the duktape-releases repo as both binaries and in unpacked form as git tags.

Have fun!

Support

About this repository

This repository is intended for Duktape developers only, and contains Duktape internals: test cases, internal documentation, sources for the duktape.org web site, etc.

Getting started: end user

When embedding Duktape in your application you should use the packaged source distributables available from duktape.org/download.html. See duktape.org/guide.html#gettingstarted for the basics.

The distributable src/ directory contains a duk_config.h configuration header and amalgamated sources for Duktape default configuration. If necessary, use python tools/configure.py to create header and sources for customized configuration options, see http://wiki.duktape.org/Configuring.html. For example, to enable fastint support (example for Linux):

$ tar xvfJ duktape-2.0.0.tar.xz
$ cd duktape-2.0.0
$ rm -rf src-custom
$ python tools/configure.py \
      --source-directory src-input \
      --output-directory src-custom \
      --config-metadata config \
      -DDUK_USE_FASTINT

# src-custom/ will now contain: duktape.c, duktape.h, duk_config.h.

You can download and install Duktape using the vcpkg dependency manager:

$ git clone https://github.com/Microsoft/vcpkg.git
$ cd vcpkg
$ ./bootstrap-vcpkg.sh
$ ./vcpkg integrate install
$ vcpkg install duktape

The Duktape port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

You can also clone this repository, make modifications, and build a source distributable on Linux, macOS, and Windows using python util/dist.py. You'll need Python 2 and Python YAML binding.

Getting started: modifying and rebuilding the distributable

If you intend to change Duktape internals and want to rebuild the source distributable in Linux, macOS, or Windows:

# Linux; can often install from packages or using 'pip'
# Install Node.js >= 16.x
$ sudo apt-get install python python-yaml
$ python util/dist.py

# macOS
# Install Python 2.7.x
# Install Node.js >= 16.x
$ pip install PyYAML
$ python util/dist.py

# Windows
; Install Python 2.7.x from python.org, and add it to PATH
; Install Node.js >= 16.x
> pip install PyYAML
> python util\dist.py

The source distributable directory will be in dist/.

For platform specific notes see http://wiki.duktape.org/DevelopmentSetup.html.

Getting started: other development (Linux only)

Other development stuff, such as building the website and running test cases, is based on a Makefile supported for Linux x86-64 only.

There are some Docker images which can simplify the development setup and also document the needed packages. These are also supported for Linux x86-64 only. For example:

# Build Docker images.  This takes a long time.
$ make docker-images

# Equivalent of 'make dist-source', but runs inside a container.
$ make docker-dist-source-wd

# Run a shell with /work/duktape containing a disposable master snapshot.
$ make docker-shell-master

# Run a shell with /work/duktape mounted from current directory.
# This allows editing, building, testing, etc with an interactive
# shell running in the container.
$ make docker-shell-wdmount

# For non-native images you may need:
# https://github.com/multiarch/qemu-user-static

There is limited support for developing on macOS via Docker. On Apple M1:

$ make docker-images-arm64
$ DOCKER_ARCH=arm64 make docker-shell-wdmount

Branch policy

  • The master branch is used for active development. Even though pull requests are tested before merging, master may still be broken from time to time. When development on a new major release starts, master will also get API incompatible changes without warning. For these reasons you should generally not depend on the master branch for building your project; use a release tag (e.g. v2.4.0) or a release maintenance branch (e.g. v2.4-maintenance or v2-maintenance) instead.

  • Pull requests and their related branches are frequently rebased so you should not fork off them. Pull requests may be open for a while for testing and discussion.

  • Release tags like v1.4.1 are used for releases and match the released distributables. These are stable once the release is complete.

  • Maintenance branches are used for backporting fixes and features for maintenance releases. Documentation changes go to master for maintenance releases too. For example, v1.5-maintenance was created for the 1.5.0 release and is used for 1.5.x maintenance releases.

  • A maintenance branch is also created for a major release when master moves on to active development of the next major release. For example, v1-maintenance was created when 1.5.0 was released (last planned 1.x release) and development of 2.0.0 (with API incompatible changes) started on master. The 1.6.0 and 1.7.0 releases were made from v1-maintenance for example.

Versioning

Duktape uses Semantic Versioning for official releases. Builds from Duktape repo are not official releases and don't follow strict semver, mainly because DUK_VERSION needs to have some compromise value that won't be strictly semver conforming. Because Duktape tracks the latest ECMAScript specification versions, compliance fixes are made in minor versions even when they are technically not backwards compatible. See Versioning for details.

Reporting bugs

See CONTRIBUTING.md and SECURITY.md.

Contributing

See CONTRIBUTING.md.

Copyright and license

See AUTHORS.rst and LICENSE.txt.

Duktape Wiki is part of Duktape documentation and under the same copyright and license.

NPM DownloadsLast 30 Days