예제 #1
0
파일: jsapi.c 프로젝트: rdrpenguin04/TIC-80
static duk_ret_t duk_map(duk_context* duk)
{
	s32 x = duk_is_null_or_undefined(duk, 0) ? 0 : duk_to_int(duk, 0);
	s32 y = duk_is_null_or_undefined(duk, 1) ? 0 : duk_to_int(duk, 1);
	s32 w = duk_is_null_or_undefined(duk, 2) ? TIC_MAP_SCREEN_WIDTH : duk_to_int(duk, 2);
	s32 h = duk_is_null_or_undefined(duk, 3) ? TIC_MAP_SCREEN_HEIGHT : duk_to_int(duk, 3);
	s32 sx = duk_is_null_or_undefined(duk, 4) ? 0 : duk_to_int(duk, 4);
	s32 sy = duk_is_null_or_undefined(duk, 5) ? 0 : duk_to_int(duk, 5);
	u8 chromakey = duk_is_null_or_undefined(duk, 6) ? -1 : duk_to_int(duk, 6);
	s32 scale = duk_is_null_or_undefined(duk, 7) ? 1 : duk_to_int(duk, 7);

	tic_mem* memory = (tic_mem*)getDukMachine(duk);

	if (duk_is_null_or_undefined(duk, 8))
		memory->api.map(memory, &memory->ram.map, &memory->ram.tiles, x, y, w, h, sx, sy, chromakey, scale);
	else
	{
		void* remap = duk_get_heapptr(duk, 8);

	 	RemapData data = {duk, remap};

	 	memory->api.remap((tic_mem*)getDukMachine(duk), &memory->ram.map, &memory->ram.tiles, x, y, w, h, sx, sy, chromakey, scale, remapCallback, &data);
	}

	return 0;
}
static duk_ret_t test_api_example(duk_context *ctx) {
	void *ptr;

	duk_eval_string(ctx, "({ foo: 'bar' })");
	ptr = duk_get_heapptr(ctx, -1);

	duk_put_global_string(ctx, "ref");
	duk_set_top(ctx, 0);

	duk_push_heapptr(ctx, ptr);
	duk_get_prop_string(ctx, -1, "foo");
	printf("obj.foo: %s\n", duk_safe_to_string(ctx, -1));  /* prints 'bar' */

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
예제 #3
0
void GCObjectPool::addNewList() {
    Isolate *isolate = Isolate::GetCurrent();
    duk_context *ctx = isolate->GetDukContext();

    // Get the "refs" array in the heap stash
    duk_push_global_stash(ctx);
    duk_get_prop_string(ctx, -1, "__object_list");
    duk_remove(ctx, -2);

    int type = duk_get_type(ctx, -1);

    int freeSlot;

    // freeSlot = scopeList[0]
    duk_get_prop_index(ctx, -1, 0);                                // <scopeList> <scopeList[0]>
    freeSlot = duk_get_int(ctx, -1);
    duk_pop(ctx);                                                  // <scopeList>

    if (freeSlot != 0) {
        // scopeList[0] = scopeList[freeSlot]
        duk_get_prop_index(ctx, -1, (duk_uarridx_t) freeSlot);    // <scopeList> <scopeList[freeSlot]>
        duk_put_prop_index(ctx, -2, 0);                            // <scopeList>
    } else {
        // freeSlot = scopeList.length;
        freeSlot = (int) duk_get_length(ctx, -1);
    }

    duk_push_array(ctx);
    list_ptr_ = duk_get_heapptr(ctx, -1);                         // <scopeList> <scope>

    // scopeList[freeSlot] = scope
    duk_put_prop_index(ctx, -2, (duk_uarridx_t) freeSlot);        // <scopeList>

    // Remove the refs array from the stack.
    duk_pop(ctx);                                                  //

    list_idx_ = freeSlot;
}
예제 #4
0
static duk_ret_t test_func(duk_context *ctx, void *udata) {
	(void) udata;

	if (ctx) {
		printf("dummy - return here\n"); fflush(stdout);
		return 0;
	}

	/* Up-to-date for Duktape 1.3.0, alphabetical order:
	 * $ cd website/api; ls *.yaml
	 */

	(void) duk_alloc_raw(ctx, 0);
	(void) duk_alloc(ctx, 0);
	(void) duk_base64_decode(ctx, 0);
	(void) duk_base64_encode(ctx, 0);
	(void) duk_buffer_to_string(ctx, 0);
	(void) duk_call_method(ctx, 0);
	(void) duk_call_prop(ctx, 0, 0);
	(void) duk_call(ctx, 0);
	(void) duk_char_code_at(ctx, 0, 0);
	(void) duk_check_stack_top(ctx, 0);
	(void) duk_check_stack(ctx, 0);
	(void) duk_check_type_mask(ctx, 0, 0);
	(void) duk_check_type(ctx, 0, 0);
	(void) duk_compact(ctx, 0);
	(void) duk_compile_lstring_filename(ctx, 0, "dummy", 0);
	(void) duk_compile_lstring(ctx, 0, "dummy", 0);
	(void) duk_compile_string_filename(ctx, 0, "dummy");
	(void) duk_compile_string(ctx, 0, "dummy");
	(void) duk_compile(ctx, 0);
	(void) duk_concat(ctx, 0);
	(void) duk_config_buffer(ctx, 0, NULL, 0);
	(void) duk_copy(ctx, 0, 0);
	(void) duk_create_heap_default();
	(void) duk_create_heap(NULL, NULL, NULL, NULL, NULL);
	(void) duk_debugger_attach(ctx, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	(void) duk_debugger_cooperate(ctx);
	(void) duk_debugger_detach(ctx);
	(void) duk_debugger_notify(ctx, 0);
	(void) duk_debugger_pause(ctx);
	(void) duk_decode_string(ctx, 0, NULL, NULL);
	(void) duk_def_prop(ctx, 0, 0);
	(void) duk_del_prop_index(ctx, 0, 0);
	(void) duk_del_prop_string(ctx, 0, "dummy");
	(void) duk_del_prop(ctx, 0);
	(void) duk_destroy_heap(ctx);
	(void) duk_dump_function(ctx);
	(void) duk_dup_top(ctx);
	(void) duk_dup(ctx, 0);
	(void) duk_enum(ctx, 0, 0);
	(void) duk_equals(ctx, 0, 0);
	duk_error_va(ctx, 0, NULL, NULL);
	duk_error(ctx, 0, "dummy");  /* (void) cast won't work without variadic macros */
	(void) duk_eval_lstring_noresult(ctx, "dummy", 0);
	(void) duk_eval_lstring(ctx, "dummy", 0);
	(void) duk_eval_noresult(ctx);
	(void) duk_eval_string_noresult(ctx, "dummy");
	(void) duk_eval_string(ctx, "dummy");
	(void) duk_eval(ctx);
	(void) duk_fatal(ctx, "dummy");
	(void) duk_free_raw(ctx, NULL);
	(void) duk_free(ctx, NULL);
	(void) duk_gc(ctx, 0);
	(void) duk_get_boolean(ctx, 0);
	(void) duk_get_buffer_data(ctx, 0, NULL);
	(void) duk_get_buffer(ctx, 0, NULL);
	(void) duk_get_c_function(ctx, 0);
	(void) duk_get_context(ctx, 0);
	(void) duk_get_current_magic(ctx);
	(void) duk_get_error_code(ctx, 0);
	(void) duk_get_finalizer(ctx, 0);
	(void) duk_get_global_string(ctx, 0);
	(void) duk_get_heapptr(ctx, 0);
	(void) duk_get_int(ctx, 0);
	(void) duk_get_length(ctx, 0);
	(void) duk_get_lstring(ctx, 0, NULL);
	(void) duk_get_magic(ctx, 0);
	(void) duk_get_memory_functions(ctx, NULL);
	(void) duk_get_number(ctx, 0);
	(void) duk_get_pointer(ctx, 0);
	(void) duk_get_prop_index(ctx, 0, 0);
	(void) duk_get_prop_string(ctx, 0, "dummy");
	(void) duk_get_prop(ctx, 0);
	(void) duk_get_prototype(ctx, 0);
	(void) duk_get_string(ctx, 0);
	(void) duk_get_top_index(ctx);
	(void) duk_get_top(ctx);
	(void) duk_get_type_mask(ctx, 0);
	(void) duk_get_type(ctx, 0);
	(void) duk_get_uint(ctx, 0);
	(void) duk_has_prop_index(ctx, 0, 0);
	(void) duk_has_prop_string(ctx, 0, "dummy");
	(void) duk_has_prop(ctx, 0);
	(void) duk_hex_decode(ctx, 0);
	(void) duk_hex_encode(ctx, 0);
	(void) duk_insert(ctx, 0);
	(void) duk_instanceof(ctx, 0, 0);
	(void) duk_is_array(ctx, 0);
	(void) duk_is_boolean(ctx, 0);
	(void) duk_is_bound_function(ctx, 0);
	(void) duk_is_buffer(ctx, 0);
	(void) duk_is_callable(ctx, 0);
	(void) duk_is_c_function(ctx, 0);
	(void) duk_is_constructor_call(ctx);
	(void) duk_is_dynamic_buffer(ctx, 0);
	(void) duk_is_ecmascript_function(ctx, 0);
	(void) duk_is_error(ctx, 0);
	(void) duk_is_eval_error(ctx, 0);
	(void) duk_is_fixed_buffer(ctx, 0);
	(void) duk_is_function(ctx, 0);
	(void) duk_is_lightfunc(ctx, 0);
	(void) duk_is_nan(ctx, 0);
	(void) duk_is_null_or_undefined(ctx, 0);
	(void) duk_is_null(ctx, 0);
	(void) duk_is_number(ctx, 0);
	(void) duk_is_object_coercible(ctx, 0);
	(void) duk_is_object(ctx, 0);
	(void) duk_is_pointer(ctx, 0);
	(void) duk_is_primitive(ctx, 0);
	(void) duk_is_range_error(ctx, 0);
	(void) duk_is_reference_error(ctx, 0);
	(void) duk_is_strict_call(ctx);
	(void) duk_is_string(ctx, 0);
	(void) duk_is_syntax_error(ctx, 0);
	(void) duk_is_thread(ctx, 0);
	(void) duk_is_type_error(ctx, 0);
	(void) duk_is_undefined(ctx, 0);
	(void) duk_is_uri_error(ctx, 0);
	(void) duk_is_valid_index(ctx, 0);
	(void) duk_join(ctx, 0);
	(void) duk_json_decode(ctx, 0);
	(void) duk_json_encode(ctx, 0);
	(void) duk_load_function(ctx);
	(void) duk_map_string(ctx, 0, NULL, NULL);
	(void) duk_new(ctx, 0);
	(void) duk_next(ctx, 0, 0);
	(void) duk_normalize_index(ctx, 0);
	(void) duk_pcall_method(ctx, 0);
	(void) duk_pcall_prop(ctx, 0, 0);
	(void) duk_pcall(ctx, 0);
	(void) duk_pcompile_lstring_filename(ctx, 0, "dummy", 0);
	(void) duk_pcompile_lstring(ctx, 0, "dummy", 0);
	(void) duk_pcompile_string_filename(ctx, 0, "dummy");
	(void) duk_pcompile_string(ctx, 0, "dummy");
	(void) duk_pcompile(ctx, 0);
	(void) duk_peval_lstring_noresult(ctx, "dummy", 0);
	(void) duk_peval_lstring(ctx, "dummy", 0);
	(void) duk_peval_noresult(ctx);
	(void) duk_peval_string_noresult(ctx, "dummy");
	(void) duk_peval_string(ctx, "dummy");
	(void) duk_peval(ctx);
	(void) duk_pnew(ctx, 0);
	(void) duk_pop_2(ctx);
	(void) duk_pop_3(ctx);
	(void) duk_pop_n(ctx, 0);
	(void) duk_pop(ctx);
	(void) duk_push_array(ctx);
	(void) duk_push_boolean(ctx, 0);
	(void) duk_push_buffer_object(ctx, 0, 0, 0, 0);
	(void) duk_push_buffer(ctx, 0, 0);
	(void) duk_push_c_function(ctx, NULL, 0);
	(void) duk_push_c_lightfunc(ctx, NULL, 0, 0, 0);
	(void) duk_push_context_dump(ctx);
	(void) duk_push_current_function(ctx);
	(void) duk_push_current_thread(ctx);
	(void) duk_push_dynamic_buffer(ctx, 0);
	(void) duk_push_error_object_va(ctx, 0, NULL, NULL);
	(void) duk_push_error_object(ctx, 0, "dummy");
	(void) duk_push_external_buffer(ctx);
	(void) duk_push_false(ctx);
	(void) duk_push_fixed_buffer(ctx, 0);
	(void) duk_push_global_object(ctx);
	(void) duk_push_global_stash(ctx);
	(void) duk_push_heap_stash(ctx);
	(void) duk_push_heapptr(ctx, NULL);
	(void) duk_push_int(ctx, 0);
	(void) duk_push_lstring(ctx, "dummy", 0);
	(void) duk_push_nan(ctx);
	(void) duk_push_null(ctx);
	(void) duk_push_number(ctx, 0.0);
	(void) duk_push_object(ctx);
	(void) duk_push_pointer(ctx, NULL);
	(void) duk_push_sprintf(ctx, "dummy");
	(void) duk_push_string(ctx, "dummy");
	(void) duk_push_this(ctx);
	(void) duk_push_thread_new_globalenv(ctx);
	(void) duk_push_thread_stash(ctx, NULL);
	(void) duk_push_thread(ctx);
	(void) duk_push_true(ctx);
	(void) duk_push_uint(ctx, 0);
	(void) duk_push_undefined(ctx);
	(void) duk_push_vsprintf(ctx, "dummy", NULL);
	(void) duk_put_function_list(ctx, 0, NULL);
	(void) duk_put_global_string(ctx, NULL);
	(void) duk_put_number_list(ctx, 0, NULL);
	(void) duk_put_prop_index(ctx, 0, 0);
	(void) duk_put_prop_string(ctx, 0, "dummy");
	(void) duk_put_prop(ctx, 0);
	(void) duk_realloc_raw(ctx, NULL, 0);
	(void) duk_realloc(ctx, NULL, 0);
	(void) duk_remove(ctx, 0);
	(void) duk_replace(ctx, 0);
	(void) duk_require_boolean(ctx, 0);
	(void) duk_require_buffer_data(ctx, 0, NULL);
	(void) duk_require_buffer(ctx, 0, NULL);
	(void) duk_require_c_function(ctx, 0);
	(void) duk_require_callable(ctx, 0);
	(void) duk_require_context(ctx, 0);
	(void) duk_require_function(ctx, 0);
	(void) duk_require_heapptr(ctx, 0);
	(void) duk_require_int(ctx, 0);
	(void) duk_require_lstring(ctx, 0, NULL);
	(void) duk_require_normalize_index(ctx, 0);
	(void) duk_require_null(ctx, 0);
	(void) duk_require_number(ctx, 0);
	(void) duk_require_object_coercible(ctx, 0);
	(void) duk_require_pointer(ctx, 0);
	(void) duk_require_stack_top(ctx, 0);
	(void) duk_require_stack(ctx, 0);
	(void) duk_require_string(ctx, 0);
	(void) duk_require_top_index(ctx);
	(void) duk_require_type_mask(ctx, 0, 0);
	(void) duk_require_uint(ctx, 0);
	(void) duk_require_undefined(ctx, 0);
	(void) duk_require_valid_index(ctx, 0);
	(void) duk_resize_buffer(ctx, 0, 0);
	(void) duk_safe_call(ctx, NULL, NULL, 0, 0);
	(void) duk_safe_to_lstring(ctx, 0, NULL);
	(void) duk_safe_to_string(ctx, 0);
	(void) duk_set_finalizer(ctx, 0);
	(void) duk_set_global_object(ctx);
	(void) duk_set_magic(ctx, 0, 0);
	(void) duk_set_prototype(ctx, 0);
	(void) duk_set_top(ctx, 0);
	(void) duk_steal_buffer(ctx, 0, NULL);
	(void) duk_strict_equals(ctx, 0, 0);
	(void) duk_substring(ctx, 0, 0, 0);
	(void) duk_swap_top(ctx, 0);
	(void) duk_swap(ctx, 0, 0);
	(void) duk_throw(ctx);
	(void) duk_to_boolean(ctx, 0);
	(void) duk_to_buffer(ctx, 0, NULL);
	(void) duk_to_defaultvalue(ctx, 0, 0);
	(void) duk_to_dynamic_buffer(ctx, 0, NULL);
	(void) duk_to_fixed_buffer(ctx, 0, NULL);
	(void) duk_to_int32(ctx, 0);
	(void) duk_to_int(ctx, 0);
	(void) duk_to_lstring(ctx, 0, NULL);
	(void) duk_to_null(ctx, 0);
	(void) duk_to_number(ctx, 0);
	(void) duk_to_object(ctx, 0);
	(void) duk_to_pointer(ctx, 0);
	(void) duk_to_primitive(ctx, 0, 0);
	(void) duk_to_string(ctx, 0);
	(void) duk_to_uint16(ctx, 0);
	(void) duk_to_uint32(ctx, 0);
	(void) duk_to_uint(ctx, 0);
	(void) duk_to_undefined(ctx, 0);
	(void) duk_trim(ctx, 0);
	(void) duk_xcopy_top(ctx, NULL, 0);
	(void) duk_xmove_top(ctx, NULL, 0);

	printf("never here\n"); fflush(stdout);
	return 0;
}
예제 #5
0
void
_gum_duk_args_parse (const GumDukArgs * args,
                     const gchar * format,
                     ...)
{
  duk_context * ctx = args->ctx;
  GumDukCore * core = args->core;
  va_list ap;
  duk_idx_t arg_index;
  const gchar * t;
  gboolean is_required;
  GSList * byte_arrays = NULL;
  const gchar * error_message = NULL;

  va_start (ap, format);

  arg_index = 0;
  is_required = TRUE;
  for (t = format; *t != '\0'; t++)
  {
    if (*t == '|')
    {
      is_required = FALSE;
      continue;
    }

    if (arg_index >= duk_get_top (ctx) || duk_is_undefined (ctx, arg_index))
    {
      if (is_required)
        goto missing_argument;
      else
        break;
    }

    switch (*t)
    {
      case 'i':
      {
        if (!duk_is_number (ctx, arg_index))
          goto expected_int;

        *va_arg (ap, gint *) = duk_require_int (ctx, arg_index);

        break;
      }
      case 'u':
      {
        guint u;

        if (!_gum_duk_get_uint (ctx, arg_index, &u))
          goto expected_uint;

        *va_arg (ap, guint *) = (guint) u;

        break;
      }
      case 'q':
      {
        gint64 i;
        gboolean is_fuzzy;

        is_fuzzy = t[1] == '~';
        if (is_fuzzy)
          t++;

        if (is_fuzzy)
        {
          if (!_gum_duk_parse_int64 (ctx, arg_index, core, &i))
            goto expected_int;
        }
        else
        {
          if (!_gum_duk_get_int64 (ctx, arg_index, core, &i))
            goto expected_int;
        }

        *va_arg (ap, gint64 *) = i;

        break;
      }
      case 'Q':
      {
        guint64 u;
        gboolean is_fuzzy;

        is_fuzzy = t[1] == '~';
        if (is_fuzzy)
          t++;

        if (is_fuzzy)
        {
          if (!_gum_duk_parse_uint64 (ctx, arg_index, core, &u))
            goto expected_uint;
        }
        else
        {
          if (!_gum_duk_get_uint64 (ctx, arg_index, core, &u))
            goto expected_uint;
        }

        *va_arg (ap, guint64 *) = u;

        break;
      }
      case 'z':
      {
        gssize value;

        if (duk_is_number (ctx, arg_index))
        {
          value = (gssize) duk_require_int (ctx, arg_index);
        }
        else
        {
          duk_push_heapptr (ctx, core->int64);
          duk_push_heapptr (ctx, core->uint64);

          if (duk_instanceof (ctx, arg_index, -2))
          {
            GumDukInt64 * object;

            object = _gum_duk_require_data (ctx, arg_index);

            value = (gssize) object->value;
          }
          else if (duk_instanceof (ctx, arg_index, -1))
          {
            GumDukUInt64 * object;

            object = _gum_duk_require_data (ctx, arg_index);

            value = (gssize) object->value;
          }
          else
          {
            goto expected_int;
          }

          duk_pop_2 (ctx);
        }

        *va_arg (ap, gssize *) = value;

        break;
      }
      case 'Z':
      {
        gsize value;

        if (duk_is_number (ctx, arg_index))
        {
          duk_double_t number;

          number = duk_require_number (ctx, arg_index);
          if (number < 0)
            goto expected_uint;

          value = (gsize) number;
        }
        else
        {
          duk_push_heapptr (ctx, core->int64);
          duk_push_heapptr (ctx, core->uint64);

          if (duk_instanceof (ctx, arg_index, -1))
          {
            GumDukUInt64 * object;

            object = _gum_duk_require_data (ctx, arg_index);

            value = (gsize) object->value;
          }
          else if (duk_instanceof (ctx, arg_index, -2))
          {
            GumDukInt64 * object;

            object = _gum_duk_require_data (ctx, arg_index);
            if (object->value < 0)
              goto expected_uint;

            value = (gsize) object->value;
          }
          else
          {
            goto expected_uint;
          }

          duk_pop_2 (ctx);
        }

        *va_arg (ap, gsize *) = value;

        break;
      }
      case 'n':
      {
        if (!duk_is_number (ctx, arg_index))
          goto expected_number;

        *va_arg (ap, gdouble *) = duk_require_number (ctx, arg_index);

        break;
      }
      case 'p':
      {
        gpointer ptr;
        gboolean is_fuzzy;

        is_fuzzy = t[1] == '~';
        if (is_fuzzy)
          t++;

        if (is_fuzzy)
        {
          if (!_gum_duk_parse_pointer (ctx, arg_index, core, &ptr))
            goto expected_pointer;
        }
        else
        {
          if (!_gum_duk_get_pointer (ctx, arg_index, core, &ptr))
            goto expected_pointer;
        }

        *va_arg (ap, gpointer *) = ptr;

        break;
      }
      case 's':
      {
        const gchar * str;
        gboolean is_nullable;

        is_nullable = t[1] == '?';
        if (is_nullable)
          t++;

        if (is_nullable && duk_is_null (ctx, arg_index))
          str = NULL;
        else if ((str = duk_get_string (ctx, arg_index)) == NULL)
          goto expected_string;

        *va_arg (ap, const gchar **) = str;

        break;
      }
      case 'm':
      {
        GumPageProtection prot;

        if (!_gum_duk_parse_protection (ctx, arg_index, &prot))
          goto expected_protection;

        *va_arg (ap, GumPageProtection *) = prot;

        break;
      }
      case 'V':
      {
        GumDukHeapPtr value;

        value = duk_get_heapptr (ctx, arg_index);
        if (value == NULL)
          goto expected_heap_pointer;

        *va_arg (ap, GumDukHeapPtr *) = value;

        break;
      }
      case 'O':
      {
        if (!duk_is_object (ctx, arg_index))
          goto expected_object;

        *va_arg (ap, GumDukHeapPtr *) = duk_require_heapptr (ctx, arg_index);

        break;
      }
      case 'A':
      {
        GumDukHeapPtr array;
        gboolean is_nullable;

        is_nullable = t[1] == '?';
        if (is_nullable)
          t++;

        if (duk_is_array (ctx, arg_index))
          array = duk_require_heapptr (ctx, arg_index);
        else if (is_nullable && duk_is_null (ctx, arg_index))
          array = NULL;
        else
          goto expected_array;

        *va_arg (ap, GumDukHeapPtr *) = array;

        break;
      }
      case 'F':
      {
        GumDukHeapPtr func;
        gboolean is_expecting_object, is_nullable;

        is_expecting_object = t[1] == '{';
        if (is_expecting_object)
          t += 2;

        if (is_expecting_object)
        {
          const gchar * next, * end, * t_end;

          if (!duk_is_object (ctx, arg_index))
            goto expected_callback_object;

          do
          {
            gchar name[64];
            gsize length;

            next = strchr (t, ',');
            end = strchr (t, '}');
            t_end = (next != NULL && next < end) ? next : end;
            length = t_end - t;
            strncpy (name, t, length);

            is_nullable = name[length - 1] == '?';
            if (is_nullable)
              name[length - 1] = '\0';
            else
              name[length] = '\0';

            duk_get_prop_string (ctx, arg_index, name);
            if (duk_is_function (ctx, -1))
            {
              func = duk_require_heapptr (ctx, -1);
            }
            else if (is_nullable && duk_is_null_or_undefined (ctx, -1))
            {
              func = NULL;
            }
            else
            {
              duk_pop (ctx);
              goto expected_callback_value;
            }
            duk_pop (ctx);

            *va_arg (ap, GumDukHeapPtr *) = func;

            t = t_end + 1;
          }
          while (t_end != end);

          t--;
        }
        else
        {
          is_nullable = t[1] == '?';
          if (is_nullable)
            t++;

          if (duk_is_function (ctx, arg_index))
            func = duk_require_heapptr (ctx, arg_index);
          else if (is_nullable && duk_is_null (ctx, arg_index))
            func = NULL;
          else
            goto expected_function;

          *va_arg (ap, GumDukHeapPtr *) = func;
        }

        break;
      }
      case 'B':
      {
        GBytes * bytes;
        gboolean is_nullable;

        is_nullable = t[1] == '?';
        if (is_nullable)
          t++;

        if (is_nullable && duk_is_null (ctx, arg_index))
          bytes = NULL;
        else if (!_gum_duk_parse_bytes (ctx, arg_index, &bytes))
          goto expected_bytes;

        *va_arg (ap, GBytes **) = bytes;

        if (bytes != NULL)
          byte_arrays = g_slist_prepend (byte_arrays, bytes);

        break;
      }
      case 'C':
      {
        GumCpuContext * cpu_context;
        gboolean is_nullable;

        is_nullable = t[1] == '?';
        if (is_nullable)
          t++;

        if (is_nullable && duk_is_null (ctx, arg_index))
          cpu_context = NULL;
        else if ((cpu_context = _gum_duk_get_cpu_context (ctx, arg_index,
            core)) == NULL)
          goto expected_cpu_context;

        *va_arg (ap, GumCpuContext **) = cpu_context;

        break;
      }
      default:
        g_assert_not_reached ();
    }

    arg_index++;
  }

  va_end (ap);

  g_slist_free (byte_arrays);

  return;

missing_argument:
  {
    error_message = "missing argument";
    goto error;
  }
expected_int:
  {
    error_message = "expected an integer";
    goto error;
  }
expected_uint:
  {
    error_message = "expected an unsigned integer";
    goto error;
  }
expected_number:
  {
    error_message = "expected a number";
    goto error;
  }
expected_pointer:
  {
    error_message = "expected a pointer";
    goto error;
  }
expected_string:
  {
    error_message = "expected a string";
    goto error;
  }
expected_protection:
  {
    error_message = "expected a string specifying memory protection";
    goto error;
  }
expected_heap_pointer:
  {
    error_message = "expected a heap-allocated object";
    goto error;
  }
expected_object:
  {
    error_message = "expected an object";
    goto error;
  }
expected_array:
  {
    error_message = "expected an array";
    goto error;
  }
expected_callback_object:
  {
    error_message = "expected an object containing callbacks";
    goto error;
  }
expected_callback_value:
  {
    error_message = "expected a callback value";
    goto error;
  }
expected_function:
  {
    error_message = "expected a function";
    goto error;
  }
expected_bytes:
  {
    error_message = "expected a buffer-like object";
    goto error;
  }
expected_cpu_context:
  {
    error_message = "expected a CpuContext object";
    goto error;
  }
error:
  {
    va_end (ap);

    g_slist_foreach (byte_arrays, (GFunc) g_bytes_unref, NULL);
    g_slist_free (byte_arrays);

    g_assert (error_message != NULL);
    _gum_duk_throw (ctx, error_message);
  }
}
static duk_ret_t test_basic(duk_context *ctx) {
	duk_idx_t i, n;
	void *ptr;
	void *p1, *p2, *p3;
	duk_int_t ret;

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_boolean(ctx, 1);
	duk_push_number(ctx, 123.0);
	duk_push_string(ctx, "test string");
	duk_eval_string(ctx, "({ foo: 'bar' })");
	duk_eval_string(ctx, "Duktape.dec('hex', 'deadbeef');");  /* buffer */

	/* Print pointer NULL/non-NULL */

	n = duk_get_top(ctx);
	for (i = 0; i < n + 1; i++) {
		/* Note: access i == n on purpose (invalid index) */

		ptr = duk_get_heapptr(ctx, i);

		printf("top: %ld\n", (long) duk_get_top(ctx));

		printf("idx %ld: type %ld, duk_get_heapptr() -> %s\n",
		       (long) i, (long) duk_get_type(ctx, i), (ptr ? "non-NULL" : "NULL"));

		duk_push_uint(ctx, (duk_uint_t) i);
		ret = duk_safe_call(ctx, raw_require_heapptr, 1 /*nargs*/, 1 /*nrets*/);
		if (ret == DUK_EXEC_SUCCESS) {
			;
		} else {
			printf("idx %ld: type %ld, duk_require_heapptr() -> %s\n",
			       (long) i, (long) duk_get_type(ctx, i), duk_safe_to_string(ctx, -1));
		}
		duk_pop(ctx);
	}

	/* Write the values to the global stash to simulate whatever
	 * reachability mechanisms user code uses.
	 */

	n = duk_get_top(ctx);
	duk_push_global_stash(ctx);
	for (i = 0; i < n; i++) {
		duk_dup(ctx, i);
		duk_put_prop_index(ctx, -2, (duk_uarridx_t) i);
	}

	/* Get borrowed references */

	p1 = duk_get_heapptr(ctx, 4);
	p2 = duk_get_heapptr(ctx, 5);
	p3 = duk_get_heapptr(ctx, 6);

	/* Erase value stack, simulating user code moving on and relying on
	 * stashed values keeping the target values reachable.  Force a GC
	 * for good measure.
	 */

	duk_set_top(ctx, 0);
	duk_gc(ctx, 0);

	/* Push the values back one by one and test that they're intact. */

	duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', v)); })");

	duk_dup_top(ctx);
	duk_push_heapptr(ctx, p1);
	duk_call(ctx, 1);
	duk_pop(ctx);

	duk_dup_top(ctx);
	duk_push_heapptr(ctx, p2);
	duk_call(ctx, 1);
	duk_pop(ctx);

	duk_dup_top(ctx);
	duk_push_heapptr(ctx, p3);
	duk_call(ctx, 1);
	duk_pop(ctx);

	duk_dup_top(ctx);
	duk_push_heapptr(ctx, NULL);
	duk_call(ctx, 1);
	duk_pop(ctx);

	duk_pop(ctx);

	/* Done. */

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
예제 #7
0
int main(int argc, const char *argv[]) {
	duk_context *ctx;
	int i;
	int mode;
	void *hptr1;
	void *hptr2;
	void *hptr3;
	void *hptr4;
	void *hptr5;
	void *hptr6;
	void *hptr7;
	void *hptr8;
	void *hptr9;
	void *hptr10;

	if (argc < 2) {
		return 1;
	}
	mode = atoi(argv[1]);

	ctx = duk_create_heap_default();

	duk_push_object(ctx);

	/* Push the strings we're dealing with, get their heapptrs, and keep
	 * them reachable throughout the test.  This is necessary for the
	 * heapptr test to work.  But we also want this so that the strings
	 * won't be freed and reallocated repeatedly during the test.  That
	 * might happen in real application code too, but this test focuses
	 * on the differences between the intern checks only, assuming the
	 * string itself is already in the string table.
	 */
	duk_push_string(ctx, "foo1");
	hptr1 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo2");
	hptr2 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo3");
	hptr3 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo4");
	hptr4 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo5");
	hptr5 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo6");
	hptr6 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo7");
	hptr7 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo8");
	hptr8 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo9");
	hptr9 = duk_get_heapptr(ctx, -1);
	duk_push_string(ctx, "foo10");
	hptr10 = duk_get_heapptr(ctx, -1);

	for (i = 0; i < 10000000; i++) {
		switch (mode) {
		case 0:
			duk_push_string(ctx, "foo1");
			duk_push_string(ctx, "foo2");
			duk_push_string(ctx, "foo3");
			duk_push_string(ctx, "foo4");
			duk_push_string(ctx, "foo5");
			duk_push_string(ctx, "foo6");
			duk_push_string(ctx, "foo7");
			duk_push_string(ctx, "foo8");
			duk_push_string(ctx, "foo9");
			duk_push_string(ctx, "foo10");
			break;
		case 1:
			duk_push_lstring(ctx, "foo1", 4);
			duk_push_lstring(ctx, "foo2", 4);
			duk_push_lstring(ctx, "foo3", 4);
			duk_push_lstring(ctx, "foo4", 4);
			duk_push_lstring(ctx, "foo5", 4);
			duk_push_lstring(ctx, "foo6", 4);
			duk_push_lstring(ctx, "foo7", 4);
			duk_push_lstring(ctx, "foo8", 4);
			duk_push_lstring(ctx, "foo9", 4);
			duk_push_lstring(ctx, "foo10", 5);
			break;
		case 2:
			duk_push_literal(ctx, "foo1");
			duk_push_literal(ctx, "foo2");
			duk_push_literal(ctx, "foo3");
			duk_push_literal(ctx, "foo4");
			duk_push_literal(ctx, "foo5");
			duk_push_literal(ctx, "foo6");
			duk_push_literal(ctx, "foo7");
			duk_push_literal(ctx, "foo8");
			duk_push_literal(ctx, "foo9");
			duk_push_literal(ctx, "foo10");
			break;
		case 3:
			duk_push_heapptr(ctx, hptr1);
			duk_push_heapptr(ctx, hptr2);
			duk_push_heapptr(ctx, hptr3);
			duk_push_heapptr(ctx, hptr4);
			duk_push_heapptr(ctx, hptr5);
			duk_push_heapptr(ctx, hptr6);
			duk_push_heapptr(ctx, hptr7);
			duk_push_heapptr(ctx, hptr8);
			duk_push_heapptr(ctx, hptr9);
			duk_push_heapptr(ctx, hptr10);
			break;
		case 10:
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo1");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo2");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo3");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo4");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo5");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo6");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo7");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo8");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo9");
			duk_push_uint(ctx, 123);
			duk_put_prop_string(ctx, 0, "foo10");
			duk_push_uint(ctx, 123);
			duk_get_prop_string(ctx, 0, "foo1");
			duk_get_prop_string(ctx, 0, "foo2");
			duk_get_prop_string(ctx, 0, "foo3");
			duk_get_prop_string(ctx, 0, "foo4");
			duk_get_prop_string(ctx, 0, "foo5");
			duk_get_prop_string(ctx, 0, "foo6");
			duk_get_prop_string(ctx, 0, "foo7");
			duk_get_prop_string(ctx, 0, "foo8");
			duk_get_prop_string(ctx, 0, "foo9");
			duk_get_prop_string(ctx, 0, "foo10");
			break;
		case 11:
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo1", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo2", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo3", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo4", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo5", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo6", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo7", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo8", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo9", 4);
			duk_push_uint(ctx, 123);
			duk_put_prop_lstring(ctx, 0, "foo10", 5);
			duk_push_uint(ctx, 123);
			duk_get_prop_lstring(ctx, 0, "foo1", 4);
			duk_get_prop_lstring(ctx, 0, "foo2", 4);
			duk_get_prop_lstring(ctx, 0, "foo3", 4);
			duk_get_prop_lstring(ctx, 0, "foo4", 4);
			duk_get_prop_lstring(ctx, 0, "foo5", 4);
			duk_get_prop_lstring(ctx, 0, "foo6", 4);
			duk_get_prop_lstring(ctx, 0, "foo7", 4);
			duk_get_prop_lstring(ctx, 0, "foo8", 4);
			duk_get_prop_lstring(ctx, 0, "foo9", 4);
			duk_get_prop_lstring(ctx, 0, "foo10", 5);
			break;
		case 12:
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo1");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo2");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo3");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo4");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo5");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo6");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo7");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo8");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo9");
			duk_push_uint(ctx, 123);
			duk_put_prop_literal(ctx, 0, "foo10");
			duk_push_uint(ctx, 123);
			duk_get_prop_literal(ctx, 0, "foo1");
			duk_get_prop_literal(ctx, 0, "foo2");
			duk_get_prop_literal(ctx, 0, "foo3");
			duk_get_prop_literal(ctx, 0, "foo4");
			duk_get_prop_literal(ctx, 0, "foo5");
			duk_get_prop_literal(ctx, 0, "foo6");
			duk_get_prop_literal(ctx, 0, "foo7");
			duk_get_prop_literal(ctx, 0, "foo8");
			duk_get_prop_literal(ctx, 0, "foo9");
			duk_get_prop_literal(ctx, 0, "foo10");
			break;
		case 13:
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr1);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr2);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr3);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr4);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr5);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr6);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr7);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr8);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr9);
			duk_push_uint(ctx, 123);
			duk_put_prop_heapptr(ctx, 0, hptr10);
			duk_push_uint(ctx, 123);
			duk_get_prop_heapptr(ctx, 0, hptr1);
			duk_get_prop_heapptr(ctx, 0, hptr2);
			duk_get_prop_heapptr(ctx, 0, hptr3);
			duk_get_prop_heapptr(ctx, 0, hptr4);
			duk_get_prop_heapptr(ctx, 0, hptr5);
			duk_get_prop_heapptr(ctx, 0, hptr6);
			duk_get_prop_heapptr(ctx, 0, hptr7);
			duk_get_prop_heapptr(ctx, 0, hptr8);
			duk_get_prop_heapptr(ctx, 0, hptr9);
			duk_get_prop_heapptr(ctx, 0, hptr10);
			break;
		case 20:
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo1");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo2");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo3");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo4");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo5");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo6");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo7");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo8");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo9");
			duk_push_uint(ctx, 123);
			duk_put_global_string(ctx, "foo10");
			duk_push_uint(ctx, 123);
			duk_get_global_string(ctx, "foo1");
			duk_get_global_string(ctx, "foo2");
			duk_get_global_string(ctx, "foo3");
			duk_get_global_string(ctx, "foo4");
			duk_get_global_string(ctx, "foo5");
			duk_get_global_string(ctx, "foo6");
			duk_get_global_string(ctx, "foo7");
			duk_get_global_string(ctx, "foo8");
			duk_get_global_string(ctx, "foo9");
			duk_get_global_string(ctx, "foo10");
			break;
		case 21:
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo1", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo2", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo3", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo4", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo5", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo6", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo7", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo8", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo9", 4);
			duk_push_uint(ctx, 123);
			duk_put_global_lstring(ctx, "foo10", 5);
			duk_push_uint(ctx, 123);
			duk_get_global_lstring(ctx, "foo1", 4);
			duk_get_global_lstring(ctx, "foo2", 4);
			duk_get_global_lstring(ctx, "foo3", 4);
			duk_get_global_lstring(ctx, "foo4", 4);
			duk_get_global_lstring(ctx, "foo5", 4);
			duk_get_global_lstring(ctx, "foo6", 4);
			duk_get_global_lstring(ctx, "foo7", 4);
			duk_get_global_lstring(ctx, "foo8", 4);
			duk_get_global_lstring(ctx, "foo9", 4);
			duk_get_global_lstring(ctx, "foo10", 5);
			break;
		case 22:
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo1");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo2");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo3");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo4");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo5");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo6");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo7");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo8");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo9");
			duk_push_uint(ctx, 123);
			duk_put_global_literal(ctx, "foo10");
			duk_push_uint(ctx, 123);
			duk_get_global_literal(ctx, "foo1");
			duk_get_global_literal(ctx, "foo2");
			duk_get_global_literal(ctx, "foo3");
			duk_get_global_literal(ctx, "foo4");
			duk_get_global_literal(ctx, "foo5");
			duk_get_global_literal(ctx, "foo6");
			duk_get_global_literal(ctx, "foo7");
			duk_get_global_literal(ctx, "foo8");
			duk_get_global_literal(ctx, "foo9");
			duk_get_global_literal(ctx, "foo10");
			break;
		case 23:
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr1);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr2);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr3);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr4);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr5);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr6);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr7);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr8);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr9);
			duk_push_uint(ctx, 123);
			duk_put_global_heapptr(ctx, hptr10);
			duk_push_uint(ctx, 123);
			duk_get_global_heapptr(ctx, hptr1);
			duk_get_global_heapptr(ctx, hptr2);
			duk_get_global_heapptr(ctx, hptr3);
			duk_get_global_heapptr(ctx, hptr4);
			duk_get_global_heapptr(ctx, hptr5);
			duk_get_global_heapptr(ctx, hptr6);
			duk_get_global_heapptr(ctx, hptr7);
			duk_get_global_heapptr(ctx, hptr8);
			duk_get_global_heapptr(ctx, hptr9);
			duk_get_global_heapptr(ctx, hptr10);
			break;
		default:
			return 1;
		}

		/* Keep strings and test object. */
		duk_set_top(ctx, 11);
	}

	duk_destroy_heap(ctx);
	return 0;
}
예제 #8
0
//void *duk_get_heapptr(duk_context *ctx, duk_idx_t index);
void *aperl_duk_get_heapptr(duk_context *ctx, duk_idx_t index) {
	void *ret = duk_get_heapptr(ctx, index);
	return ret;
}
예제 #9
0
JavaScriptObject::JavaScriptObject(JavaTypeMap& typeMap, JNIEnv* env, duk_context* context,
                                   jstring name, jobjectArray methods)
    : m_name(JString(env, name).str())
    , m_context(context)
    , m_instance(nullptr)
    , m_nextFinalizer(nullptr) {
    CHECK_STACK(m_context);
    duk_push_global_object(m_context);
    if (!duk_get_prop_string(m_context, -1, m_name.c_str())) {
        duk_pop_2(m_context);
        throw std::invalid_argument("A global JavaScript object called " + m_name + " was not found");
    }

    m_instance = duk_get_heapptr(m_context, -1);
    if (m_instance == nullptr) {
        duk_pop_2(m_context);
        throw std::invalid_argument("JavaScript global called " + m_name + " is not an object");
    }

    // Make sure that the object has all of the methods we want.
    jmethodID getName = nullptr;
    const jsize numArgs = env->GetArrayLength(methods);
    for (jsize i = 0; i < numArgs; ++i) {
        auto method = env->GetObjectArrayElement(methods, i);
        if (getName == nullptr) {
            jclass methodClass = env->GetObjectClass(method);
            getName = env->GetMethodID(methodClass, "getName", "()Ljava/lang/String;");
        }

        // Sanity check that as of right now, the object we're proxying has a function with this name.
        const JString methodName(env, static_cast<jstring>(env->CallObjectMethod(method, getName)));
        if (!duk_get_prop_string(m_context, -1, methodName)) {
            duk_pop_3(m_context);
            throw std::runtime_error("JavaScript global " + m_name + " has no method called " +
                                     methodName.str());
        } else if (!duk_is_callable(m_context, -1)) {
            duk_pop_3(m_context);
            throw std::runtime_error("JavaScript property " + m_name + "." + methodName.str() +
                                     " not callable");
        }

        try {
            // Build a call wrapper that handles marshalling the arguments and return value.
            m_methods.emplace(std::make_pair(env->FromReflectedMethod(method),
                                             buildMethodBody(typeMap, env, method, methodName.str())));
        } catch (const std::invalid_argument& e) {
            duk_pop_3(m_context);
            throw std::invalid_argument("In proxied method \"" + m_name + "." + methodName.str() +
                                        "\": " + e.what());
        }

        // Pop the method property.
        duk_pop(m_context);
    }

    // Keep track of any previously registered finalizer.
    duk_get_finalizer(m_context, -1);
    m_nextFinalizer = duk_is_c_function(m_context, -1)
                      ? duk_get_c_function(m_context, -1)
                      : nullptr;
    duk_pop(m_context);
    duk_push_c_function(m_context, JavaScriptObject::finalizer, 1);
    duk_set_finalizer(m_context, -2);

    // Add 'this' to the list of pointers attached to the proxied instance.
    // TODO: don't use an array here, just use a separate hidden property for each proxy.
    if (!duk_has_prop_string(m_context, -1, WRAPPER_THIS_PROP_NAME)) {
        duk_push_array(m_context);
    } else {
        duk_get_prop_string(m_context, -1, WRAPPER_THIS_PROP_NAME);
    }

    const duk_size_t length = duk_get_length(m_context, -1);
    duk_push_pointer(m_context, this);
    duk_put_prop_index(m_context, -2, static_cast<duk_uarridx_t>(length));
    // Add the array (back) to the instance.
    duk_put_prop_string(m_context, -2, WRAPPER_THIS_PROP_NAME);

    // Pop the global and our instance.
    duk_pop_2(m_context);
}