示例#1
0
gboolean
_gum_duk_get_pointer (duk_context * ctx,
                      duk_idx_t index,
                      GumDukCore * core,
                      gpointer * ptr)
{
  gboolean success = TRUE;

  duk_dup (ctx, index);
  duk_push_heapptr (ctx, core->native_pointer);

  if (duk_is_pointer (ctx, -2))
  {
    *ptr = duk_require_pointer (ctx, -2);
  }
  else if (duk_instanceof (ctx, -2, -1))
  {
    GumDukNativePointer * p;

    p = _gum_duk_require_data (ctx, -2);

    *ptr = p->value;
  }
  else if (duk_is_object (ctx, -2))
  {
    gboolean is_native_pointer;

    duk_get_prop_string (ctx, -2, "handle");

    is_native_pointer = duk_instanceof (ctx, -1, -2);
    if (is_native_pointer)
    {
      GumDukNativePointer * p;

      p = _gum_duk_require_data (ctx, -1);

      *ptr = p->value;
    }
    else
    {
      success = FALSE;
    }

    duk_pop (ctx);
  }
  else
  {
    success = FALSE;
  }

  duk_pop_2 (ctx);

  return success;
}
示例#2
0
static duk_ret_t test_3e(duk_context *ctx, void *udata) {
	(void) udata;

	printf("%d\n", duk_instanceof(ctx, DUK_INVALID_INDEX, DUK_INVALID_INDEX));
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
示例#3
0
static duk_ret_t test_3d(duk_context *ctx, void *udata) {
	(void) udata;

	printf("%d\n", duk_instanceof(ctx, 0, 1));  /* 1 is out of stack */
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
示例#4
0
/* duk_instanceof() inherits the Ecmascript type requirements for lval and rval.
 * In particular, rval must be a -callable- object.  Here, for example, trying
 * to do the equivalent of: "new Error() instanceof new Error()" is a TypeError
 * because the rval is a non-callable object.
 */
static duk_ret_t test_2(duk_context *ctx) {
	duk_eval_string(ctx, "new Error('test error')");

	duk_instanceof(ctx, 0, 0);  /* -> TypeError */

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
示例#5
0
GumDukNativePointer *
_gum_duk_require_native_pointer (duk_context * ctx,
                                 duk_idx_t index,
                                 GumDukCore * core)
{
  duk_dup (ctx, index);
  duk_push_heapptr (ctx, core->native_pointer);
  if (!duk_instanceof (ctx, -2, -1))
    _gum_duk_throw (ctx, "expected NativePointer");
  duk_pop_2 (ctx);

  return _gum_duk_require_data (ctx, index);
}
示例#6
0
guint64
_gum_duk_require_uint64 (duk_context * ctx,
                         duk_idx_t index,
                         GumDukCore * core)
{
  GumDukUInt64 * object;

  duk_dup (ctx, index);
  duk_push_heapptr (ctx, core->uint64);
  if (!duk_instanceof (ctx, -2, -1))
    _gum_duk_throw (ctx, "expected UInt64");
  duk_pop_2 (ctx);

  object = _gum_duk_require_data (ctx, index);

  return object->value;
}
示例#7
0
/* Basic test. */
static duk_ret_t test_1(duk_context *ctx) {
	int i;

	duk_eval_string(ctx, "new RangeError('test error')");  /* 0 */
	duk_eval_string(ctx, "Error");  /* 1 */
	duk_eval_string(ctx, "RangeError");  /* 2 */
	duk_eval_string(ctx, "TypeError");  /* 3 */
	duk_eval_string(ctx, "Object");  /* 4 */
	duk_eval_string(ctx, "Function");  /* 5 */

	for (i = 1; i <= 5; i++) {
		printf("0 instanceof %d: %d\n", i, (int) duk_instanceof(ctx, 0, i));
	}

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
示例#8
0
gboolean
_gum_duk_get_uint64 (duk_context * ctx,
                     duk_idx_t index,
                     GumDukCore * core,
                     guint64 * u)
{
  if (duk_is_pointer (ctx, index))
  {
    *u = *((const guint64 *) duk_require_pointer (ctx, index));
    return TRUE;
  }
  else if (duk_is_number (ctx, index))
  {
    duk_double_t number;

    number = duk_require_number (ctx, index);
    if (number < 0)
      return FALSE;

    *u = (guint64) number;
    return TRUE;
  }
  else
  {
    gboolean success = FALSE;

    duk_dup (ctx, index);
    duk_push_heapptr (ctx, core->uint64);

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

      object = _gum_duk_require_data (ctx, -2);

      *u = object->value;
      success = TRUE;
    }

    duk_pop_2 (ctx);

    return success;
  }
}
示例#9
0
gboolean
_gum_duk_get_int64 (duk_context * ctx,
                    duk_idx_t index,
                    GumDukCore * core,
                    gint64 * i)
{
  if (duk_is_pointer (ctx, index))
  {
    *i = *((const gint64 *) duk_require_pointer (ctx, index));
    return TRUE;
  }
  else if (duk_is_number (ctx, index))
  {
    *i = (gint64) duk_require_number (ctx, index);
    return TRUE;
  }
  else
  {
    gboolean success = FALSE;

    duk_dup (ctx, index);
    duk_push_heapptr (ctx, core->int64);

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

      object = _gum_duk_require_data (ctx, -2);

      *i = object->value;
      success = TRUE;
    }

    duk_pop_2 (ctx);

    return success;
  }
}
示例#10
0
GumCpuContext *
_gum_duk_get_cpu_context (duk_context * ctx,
                          duk_idx_t index,
                          GumDukCore * core)
{
  gboolean is_cpu_context;
  GumDukCpuContext * instance;

  if (!duk_is_object (ctx, index))
    return NULL;

  duk_dup (ctx, index);
  duk_push_heapptr (ctx, core->cpu_context);
  is_cpu_context = duk_instanceof (ctx, -2, -1);
  duk_pop_2 (ctx);

  if (!is_cpu_context)
    return NULL;

  instance = _gum_duk_require_data (ctx, index);

  return instance->handle;
}
示例#11
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;
}
示例#12
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);
  }
}
//duk_bool_t duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2);
duk_bool_t aperl_duk_instanceof(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
	duk_bool_t ret = duk_instanceof(ctx, index1, index2);
	return ret;
}
示例#14
0
static duk_ret_t test_3c(duk_context *ctx) {
	printf("%d\n", duk_instanceof(ctx, 1, 0));  /* 1 is out of stack */
	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}