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.



557
558
559
560
561
562
563
564
565
566
567
568
# File 'ext/duktape/duktape_ext.c', line 557

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

#_valid?Boolean

:nodoc:

Checks that we are in a fine state

Returns:

  • (Boolean)


527
528
529
530
531
532
533
534
535
536
537
# File 'ext/duktape/duktape_ext.c', line 527

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)


494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'ext/duktape/duktape_ext.c', line 494

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

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

  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)


581
582
583
584
585
586
587
# File 'ext/duktape/duktape_ext.c', line 581

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

  return state->complex_object;
}

#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)


332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'ext/duktape/duktape_ext.c', line 332

static VALUE ctx_eval_string(int argc, VALUE *argv, VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, 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)


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'ext/duktape/duktape_ext.c', line 376

static VALUE ctx_exec_string(int argc, VALUE *argv, VALUE self)
{
  struct state *state;
  Data_Get_Struct(self, struct state, 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)


469
470
471
472
473
474
475
476
477
478
479
# File 'ext/duktape/duktape_ext.c', line 469

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

  ctx_get_nested_prop(state, prop);

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