All Projects → judofyr → duktape.rb

judofyr / duktape.rb

Licence: other
Ruby bindings to the Duktape JavaScript interpreter

Programming Languages

c
50402 projects - #5 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to duktape.rb

jispy
A JavaScript interpreter in Python, built for embedding JavaScript.
Stars: ✭ 43 (-36.76%)
Mutual labels:  javascript-interpreter
DuktapeJava
Tiny Powerfull JavaScript Engine On Android Platform integrating with java
Stars: ✭ 74 (+8.82%)
Mutual labels:  duktape
droidducky-app
Android app to run duckyscript
Stars: ✭ 34 (-50%)
Mutual labels:  javascript-interpreter
pyduktape
Embed the Duktape JS interpreter in Python
Stars: ✭ 77 (+13.24%)
Mutual labels:  duktape
sporks
The source code repository for Sporks, the learning, backchatting, scriptable discord bot!
Stars: ✭ 30 (-55.88%)
Mutual labels:  duktape
Duktape
Duktape - embeddable Javascript engine with a focus on portability and compact footprint
Stars: ✭ 5,076 (+7364.71%)
Mutual labels:  duktape
libvmod-cfg
VMOD useful to access to contents of environment variables and local or remote files from VCL, usually for configuration purposes, including execution of Lua and JavaScript programs.
Stars: ✭ 20 (-70.59%)
Mutual labels:  duktape
hs-duktape
Haskell bindings for a very compact embedded JavaScript engine
Stars: ✭ 24 (-64.71%)
Mutual labels:  duktape
Boa
Boa is an embeddable and experimental Javascript engine written in Rust. Currently, it has support for some of the language.
Stars: ✭ 2,509 (+3589.71%)
Mutual labels:  javascript-interpreter
Quickjs
QuickJS是一个小型并且可嵌入的Javascript引擎,它支持ES2020规范,包括模块,异步生成器和代理器。
Stars: ✭ 2,199 (+3133.82%)
Mutual labels:  javascript-interpreter

Duktape.rb

Duktape.rb is a C extension for the Duktape JavaScript interpreter.

Quickstart

$ rake
$ ruby example.rb

Usage

require 'duktape'

# Create a new context
ctx = Duktape::Context.new

## Evaluate a string
p ctx.eval_string('1 + 1')  # => 2

Contexts

Creating a context creates a fresh evaluation environment with no global variables or functions defined.

A common pattern is to create a new context, define static functions once, and reuse the context to invoke the function many times with call_prop.

ctx = Duktape::Context.new

ctx.exec_string <<-JS
  function process(str, options) {
    // ...
  }
JS

ctx.call_prop('process', 'some data', a: 1, b: 2)

Call APIs

  • exec_string - Evaluate a JavaScript String on the context and return nil.
  • eval_string - Evaluate a JavaScript String expression and return the result as a Ruby Object.
  • get_prop - Access the property of the global object and return the value as a Ruby Object.
  • call_prop - Call a defined function with the given parameters and return the value as a Ruby Object.

Defining functions

You can define simple functions in Ruby that can be called from JavaScript:

ctx.define_function("leftpad") do |str, n, ch=' '|
  str.rjust(n, ch)
end

Exceptions

Executing JS may raise two classes of errors: Duktape::Error and Duktape::InternalError.

Any JS runtime error that is thrown in the interpreter is converted to a Ruby Duktape::Error. Specific error subclasses, such as SyntaxError and TypeError, are mapped from JS to the Ruby equivalent of the same name.

ctx = Duktape::Context.new
ctx.exec_string <<JS
  (function() {
    throw new Error("fail");
  })();
JS
# raises Duktape::Error: fail

The second error hierarchy, Duktape::InternalError, is reserved for errors in the Duktape interpreter itself. It may be an indication of a bug in this library.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].