Class: Duktape::Context

Inherits:
Object
  • Object
show all
Defined in:
ext/duktape/duktape_ext.c

Instance Method Summary collapse

Constructor Details

#newObject #new(complex_object: obj) ⇒ Object

Returns a new JavaScript evaluation context.



655
656
657
658
659
660
661
662
663
664
665
666
# File 'ext/duktape/duktape_ext.c', line 655

static VALUE ctx_initialize(int argc, VALUE *argv, VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);

  VALUE options;
  rb_scan_args(argc, argv, ":", &options);
  if (!NIL_P(options))
    state->complex_object = rb_hash_lookup2(options, ID2SYM(id_complex_object), state->complex_object);

  return Qnil;
}

Instance Method Details

#_invoke_fatalObject

:nodoc:

Invokes duk_fatal(). Only used for testing.



614
615
616
617
618
619
620
621
622
# File 'ext/duktape/duktape_ext.c', line 614

static VALUE ctx_invoke_fatal(VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);

  duk_fatal(state->ctx, "induced fatal error");

  return Qnil;
}

#_valid?Boolean

:nodoc:

Checks that we are in a fine state

Returns:

  • (Boolean)


597
598
599
600
601
602
603
604
605
606
607
# File 'ext/duktape/duktape_ext.c', line 597

static VALUE ctx_is_valid(VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);

  if (duk_is_valid_index(state->ctx, -1)) {
    return Qfalse;
  } else {
    return Qtrue;
  }
}

#call_prop(name, params, ...) ⇒ Object #call_prop([names,...], params, ...) ⇒ Object

Call a function defined in the global scope with the given parameters. An Array of names can be given to call a function on a nested object.

ctx.call_prop("parseInt", "42")       #=> 42
ctx.call_prop(["Math", "pow"], 2, 10) #=> 1024

Overloads:

  • #call_prop(name, params, ...) ⇒ Object

    Returns:

    • (Object)
  • #call_prop([names,...], params, ...) ⇒ Object

    Returns:

    • (Object)


488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/duktape/duktape_ext.c', line 488

static VALUE ctx_call_prop(int argc, VALUE* argv, VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);
  check_fatal(state);

  VALUE prop;
  rb_scan_args(argc, argv, "1*", &prop, NULL);

  ctx_get_nested_prop(state, prop);

  // Swap receiver and function
  duk_swap_top(state->ctx, -2);

  // Push arguments
  for (int i = 1; i < argc; i++) {
    ctx_push_ruby_object(state, argv[i]);
  }

  if (duk_pcall_method(state->ctx, (argc - 1)) == DUK_EXEC_ERROR) {
    raise_ctx_error(state);
  }

  VALUE res = ctx_stack_to_value(state, -1);
  duk_set_top(state->ctx, 0);
  return res;
}

#complex_objectObject

Returns the default complex object, the value that would be returned if a JavaScript object had no representation in Ruby, such as a JavaScript Function. See also Context::new.

ctx.complex_object #=> #<Duktape::ComplexObject>

Returns:

  • (Object)


679
680
681
682
683
684
685
# File 'ext/duktape/duktape_ext.c', line 679

static VALUE ctx_complex_object(VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);

  return state->complex_object;
}

#ctx_define_function(name, &block) ⇒ nil

Define a function defined in the global scope and identified by a name.

ctx.ctx_define_function("hello_world") { |ctx| 'Hello world' } #=> nil

Returns:

  • (nil)


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'ext/duktape/duktape_ext.c', line 554

static VALUE ctx_define_function(VALUE self, VALUE prop)
{
  VALUE block;
  struct state *state;
  duk_context *ctx;

  // a block is required
  if (!rb_block_given_p())
    rb_raise(rb_eArgError, "Expected block");

  // get the context
  Data_Get_Struct(self, struct state, state);
  check_fatal(state);

  ctx = state->ctx;

  // the c function is available in the global scope
  duk_push_global_object(ctx);

  duk_push_c_function(ctx, ctx_call_pushed_function, DUK_VARARGS);

  block = rb_block_proc();
  rb_ary_push(state->blocks, block); // block will be properly garbage collected

  // both block and state are required by the pushed function
  duk_push_string(ctx, "block");
  duk_push_pointer(ctx, (void *) block);
  duk_def_prop(ctx, -3,  DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0);

  duk_push_string(ctx, "state");
  duk_push_pointer(ctx, (void *) state);
  duk_def_prop(ctx, -3,  DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0);

  duk_put_prop_string(ctx, -2, StringValueCStr(prop));

  return Qnil;
}

#eval_string(string[, filename]) ⇒ Object

Evaluate JavaScript expression within context returning the value as a Ruby object.

ctx.eval_string("40 + 2") #=> 42

Returns:

  • (Object)


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/duktape/duktape_ext.c', line 323

static VALUE ctx_eval_string(int argc, VALUE *argv, VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);
  check_fatal(state);

  VALUE source;
  VALUE filename;

  rb_scan_args(argc, argv, "11", &source, &filename);

  if (NIL_P(filename)) {
    filename = sDefaultFilename;
  }

  StringValue(source);
  StringValue(filename);

  ctx_push_ruby_object(state, source);
  ctx_push_ruby_object(state, filename);

  if (duk_pcompile(state->ctx, DUK_COMPILE_EVAL) == DUK_EXEC_ERROR) {
    raise_ctx_error(state);
  }

  if (duk_pcall(state->ctx, 0) == DUK_EXEC_ERROR) {
    raise_ctx_error(state);
  }

  VALUE res = ctx_stack_to_value(state, -1);
  duk_set_top(state->ctx, 0);
  return res;
}

#exec_string(string[, filename]) ⇒ nil

Evaluate JavaScript expression within context returning the value as a Ruby object.

ctx.exec_string("var foo = 42")
ctx.eval_string("foo") #=> 42

Returns:

  • (nil)


368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'ext/duktape/duktape_ext.c', line 368

static VALUE ctx_exec_string(int argc, VALUE *argv, VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);
  check_fatal(state);

  VALUE source;
  VALUE filename;

  rb_scan_args(argc, argv, "11", &source, &filename);

  if (NIL_P(filename)) {
    filename = sDefaultFilename;
  }

  StringValue(source);
  StringValue(filename);

  ctx_push_ruby_object(state, source);
  ctx_push_ruby_object(state, filename);

  if (duk_pcompile(state->ctx, 0) == DUK_EXEC_ERROR) {
    raise_ctx_error(state);
  }

  if (duk_pcall(state->ctx, 0) == DUK_EXEC_ERROR) {
    raise_ctx_error(state);
  }

  duk_set_top(state->ctx, 0);
  return Qnil;
}

#get_prop(name) ⇒ Object #get_prop([names,...]) ⇒ Object

Access the property of the global object. An Array of names can be given to access the property on a nested object.

ctx.exec_string("var n = 42", "foo.js")
ctx.get_prop("n") #=> 42

ctx.get_prop(["Math", "PI"]) #=> 3.14

Overloads:

  • #get_prop(name) ⇒ Object

    Returns:

    • (Object)
  • #get_prop([names,...]) ⇒ Object

    Returns:

    • (Object)


462
463
464
465
466
467
468
469
470
471
472
473
# File 'ext/duktape/duktape_ext.c', line 462

static VALUE ctx_get_prop(VALUE self, VALUE prop)
{
  struct state *state;
  Data_Get_Struct(self, struct state, state);
  check_fatal(state);

  ctx_get_nested_prop(state, prop);

  VALUE res = ctx_stack_to_value(state, -1);
  duk_set_top(state->ctx, 0);
  return res;
}