module JS

JavaScript interoperations module

The JS module provides a way to interact with JavaScript from Ruby.


require 'js'
JS.eval("return 1 + 2") # => 3[:document].write("Hello, world!")
div =[:document].createElement("div")
div[:innerText] = "click me"[:document][:body].appendChild(div)
div.addEventListener("click") do |event|
  puts event          # => # [object MouseEvent]
  puts event[:detail] # => 1
  div[:innerText] = "clicked!"

If you are using ‘ruby.wasm` without `stdlib` you will not have `addEventListener` and other specialized functions defined. You can still acomplish many of the same things using `call` instead.


require 'js'
JS.eval("return 1 + 2") # => 3[:document].call(:write, "Hello, world!")
div =[:document].call(:createElement, "div")
div[:innerText] = "click me"[:document][:body].call(:appendChild, div), "click") do |event|
  puts event          # => # [object MouseEvent]
  puts event[:detail] # => 1
  div[:innerText] = "clicked!"



Public Class Methods

eval(code) → JS::Object click to toggle source

Evaluates the given JavaScript code, returning the result as a JS::Object.

p JS.eval("return 1 + 1").to_i                             # => 2
p JS.eval("return new Object()").is_a?([:Object]) # => true
static VALUE _rb_js_eval_js(VALUE _, VALUE code_str) {
  rb_js_abi_host_string_t abi_str;
  rstring_to_abi_string(code_str, &abi_str);
  rb_js_abi_host_js_abi_result_t ret;
  rb_js_abi_host_eval_js(&abi_str, &ret);
  return jsvalue_s_new(ret.val.success);
global → JS::Object click to toggle source

Returns globalThis JavaScript object.

static VALUE _rb_js_global_this(VALUE _) {
  return jsvalue_s_new(rb_js_abi_host_global_this());
is_a?(js_obj, js_class) → true or false click to toggle source

Returns true if js_class is the instance of js_obj, otherwise returns false. Comparison is done using the instanceof in JavaScript.

p JS.is_a?(,[:Object]) #=> true
p JS.is_a?(, Object)             #=> false
static VALUE _rb_js_is_kind_of(VALUE klass, VALUE obj, VALUE c) {
  if (!IS_JSVALUE(obj)) {
    return Qfalse;
  struct jsvalue *val = DATA_PTR(obj);
  VALUE js_klass_v = _rb_js_try_convert(klass, c);
  struct jsvalue *js_klass = DATA_PTR(js_klass_v);
  return RBOOL(rb_js_abi_host_instance_of(val->abi, js_klass->abi));
promise_scheduler() click to toggle source
# File ext/js/lib/js.rb, line 82
def self.promise_scheduler
try_convert(obj) → JS::Object or nil click to toggle source

Try to convert the given object to a JS::Object using to_js method. Returns nil if the object cannot be converted.

p JS.try_convert(1)                           # => JS::Object
p JS.try_convert("foo")                       # => JS::Object
p JS.try_convert(                  # => nil
p JS.try_convert(JS::Object.wrap( # => JS::Object
VALUE _rb_js_try_convert(VALUE klass, VALUE obj) {
  if (_rb_js_is_js(klass, obj)) {
    return obj;
  } else if (rb_respond_to(obj, i_to_js)) {
    return rb_funcallv(obj, i_to_js, 0, NULL);
  } else {
    return Qnil;