Пример #1
0
void
_gum_duk_push_native_pointer (duk_context * ctx,
                              gpointer address,
                              GumDukCore * core)
{
  GumDukNativePointerImpl * ptr;

  ptr = core->cached_native_pointers;
  if (ptr != NULL)
  {
    core->cached_native_pointers = ptr->next;

    duk_push_heapptr (ctx, ptr->object);
    ptr->parent.value = address;

    duk_push_global_stash (ctx);
    duk_del_prop_string (ctx, -1, ptr->id);
    duk_pop (ctx);

    return;
  }

  duk_push_heapptr (ctx, core->native_pointer);
  duk_push_pointer (ctx, address);
  duk_new (ctx, 1);
}
Пример #2
0
void ObjectList::AddObject(void *val) {
    Isolate *isolate = Isolate::GetCurrent();
    duk_context *ctx = isolate->GetDukContext();

    duk_push_heapptr(ctx, list_ptr_);
    duk_push_heapptr(ctx, val);
    duk_size_t len = duk_get_length(ctx, -1);
    duk_put_prop_index(ctx, -2, (duk_uarridx_t) len);
    duk_pop(ctx);
}
Пример #3
0
static void
gum_duk_invocation_listener_on_enter (GumInvocationListener * listener,
                                      GumInvocationContext * ic)
{
  GumDukInvocationListener * self = GUM_DUK_INVOCATION_LISTENER_CAST (listener);

  if (gum_script_backend_is_ignoring (
      gum_invocation_context_get_thread_id (ic)))
    return;

  if (self->on_enter != NULL)
  {
    GumDukInterceptor * module = self->module;
    GumDukCore * core = module->core;
    duk_context * ctx = core->ctx;
    GumDukScope scope;
    GumDukInvocationContext * jic;
    GumDukInvocationArgs * args;

    _gum_duk_scope_enter (&scope, core);

    jic = _gum_duk_interceptor_obtain_invocation_context (module);
    _gum_duk_invocation_context_reset (jic, ic);

    args = gum_duk_interceptor_obtain_invocation_args (module);
    gum_duk_invocation_args_reset (args, ic);

    duk_push_heapptr (ctx, self->on_enter);
    duk_push_heapptr (ctx, jic->object);
    duk_push_heapptr (ctx, args->object);
    _gum_duk_scope_call_method (&scope, 1);
    duk_pop (ctx);

    gum_duk_invocation_args_reset (args, NULL);
    gum_duk_interceptor_release_invocation_args (module, args);

    _gum_duk_invocation_context_reset (jic, NULL);
    if (self->on_leave != NULL)
    {
      *GUM_LINCTX_GET_FUNC_INVDATA (ic, GumDukHeapPtr) = jic;
    }
    else
    {
      _gum_duk_interceptor_release_invocation_context (module, jic);
    }

    _gum_duk_scope_leave (&scope);
  }
}
Пример #4
0
static gboolean
gum_kernel_scan_context_emit_match (GumAddress address,
                                    gsize size,
                                    GumKernelScanContext * self)
{
  GumDukCore * core = self->core;
  GumDukScope scope;
  duk_context * ctx;
  gboolean proceed;

  ctx = _gum_duk_scope_enter (&scope, core);

  duk_push_heapptr (ctx, self->on_match);

  _gum_duk_push_uint64 (ctx, address, core);
  duk_push_number (ctx, size);

  proceed = TRUE;

  if (_gum_duk_scope_call (&scope, 2))
  {
    if (duk_is_string (ctx, -1))
      proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0;
  }
  duk_pop (ctx);

  _gum_duk_scope_leave (&scope);

  return proceed;
}
Пример #5
0
GumDukCpuContext *
_gum_duk_push_cpu_context (duk_context * ctx,
                           GumCpuContext * handle,
                           GumDukCpuContextAccess access,
                           GumDukCore * core)
{
  GumDukCpuContext * scc;

  scc = g_slice_new (GumDukCpuContext);

  duk_push_heapptr (ctx, core->cpu_context);
  duk_new (ctx, 0);
  _gum_duk_put_data (ctx, -1, scc);
  scc->object = duk_require_heapptr (ctx, -1);

  if (access == GUM_CPU_CONTEXT_READWRITE)
  {
    scc->handle = handle;
  }
  else
  {
    memcpy (&scc->storage, handle, sizeof (GumCpuContext));
    scc->handle = &scc->storage;
  }
  scc->access = access;

  return scc;
}
Пример #6
0
static gboolean
gum_duk_exception_handler_on_exception (GumExceptionDetails * details,
                                        gpointer user_data)
{
  GumDukExceptionHandler * handler = user_data;
  GumDukCore * core = handler->core;
  GumDukScope scope;
  duk_context * ctx;
  GumDukCpuContext * cpu_context;
  gboolean handled = FALSE;

  ctx = _gum_duk_scope_enter (&scope, core);

  _gum_duk_push_exception_details (ctx, details, core, &cpu_context);

  duk_push_heapptr (ctx, handler->callback);
  duk_dup (ctx, -2);
  if (_gum_duk_scope_call (&scope, 1))
  {
    if (duk_is_boolean (ctx, -1))
      handled = duk_require_boolean (ctx, -1);
  }

  _gum_duk_cpu_context_make_read_only (cpu_context);

  duk_pop_2 (ctx);

  _gum_duk_scope_leave (&scope);

  return handled;
}
Пример #7
0
static gboolean
gum_emit_malloc_range (const GumMallocRangeDetails * details,
                       gpointer user_data)
{
  GumDukMatchContext * mc = user_data;
  GumDukScope * scope = mc->scope;
  duk_context * ctx = scope->ctx;
  gboolean proceed = TRUE;

  duk_push_heapptr (ctx, mc->on_match);

  duk_push_object (ctx);

  _gum_duk_push_native_pointer (ctx,
      GSIZE_TO_POINTER (details->range->base_address), scope->core);
  duk_put_prop_string (ctx, -2, "base");

  duk_push_uint (ctx, details->range->size);
  duk_put_prop_string (ctx, -2, "size");

  if (_gum_duk_scope_call_sync (scope, 1))
  {
    if (duk_is_string (ctx, -1))
      proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0;
  }
  else
  {
    proceed = FALSE;
  }
  duk_pop (ctx);

  return proceed;
}
Пример #8
0
static gboolean
gum_emit_range (const GumRangeDetails * details,
                gpointer user_data)
{
  GumDukMatchContext * mc = user_data;
  GumDukScope * scope = mc->scope;
  duk_context * ctx = scope->ctx;
  gboolean proceed = TRUE;

  duk_push_heapptr (ctx, mc->on_match);
  _gum_duk_push_range (ctx, details, scope->core);

  if (_gum_duk_scope_call_sync (scope, 1))
  {
    if (duk_is_string (ctx, -1))
      proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0;
  }
  else
  {
    proceed = FALSE;
  }
  duk_pop (ctx);

  return proceed;
}
Пример #9
0
static void remapCallback(void* data, s32 x, s32 y, RemapResult* result)
{

	RemapData* remap = (RemapData*)data;
	duk_context* duk = remap->duk;

	duk_push_heapptr(duk, remap->remap);
	duk_push_int(duk, result->index);
	duk_push_int(duk, x);
	duk_push_int(duk, y);
	duk_pcall(duk, 3);

	if(duk_is_array(duk, -1))
	{
		duk_get_prop_index(duk, -1, 0);
		result->index = duk_to_int(duk, -1);
		duk_pop(duk);

		duk_get_prop_index(duk, -1, 1);
		result->flip = duk_to_int(duk, -1);
		duk_pop(duk);

		duk_get_prop_index(duk, -1, 2);
		result->rotate = duk_to_int(duk, -1);
		duk_pop(duk);
	}
	else
	{
		result->index = duk_to_int(duk, -1);		
	}

	duk_pop(duk);
}
Пример #10
0
static void
gum_duk_invocation_listener_on_leave (GumInvocationListener * listener,
                                      GumInvocationContext * ic)
{
  GumDukInvocationListener * self = GUM_DUK_INVOCATION_LISTENER_CAST (listener);

  if (gum_script_backend_is_ignoring (
      gum_invocation_context_get_thread_id (ic)))
    return;

  if (self->on_leave != NULL)
  {
    GumDukInterceptor * module = self->module;
    GumDukCore * core = module->core;
    duk_context * ctx = core->ctx;
    GumDukScope scope;
    GumDukInvocationContext * jic;
    GumDukInvocationReturnValue * retval;

    _gum_duk_scope_enter (&scope, core);

    jic = (self->on_enter != NULL)
        ? *GUM_LINCTX_GET_FUNC_INVDATA (ic, GumDukInvocationContext *)
        : NULL;
    if (jic == NULL)
    {
      jic = _gum_duk_interceptor_obtain_invocation_context (module);
    }
    _gum_duk_invocation_context_reset (jic, ic);

    retval = gum_duk_interceptor_obtain_invocation_return_value (module);
    gum_duk_invocation_return_value_reset (retval, ic);

    duk_push_heapptr (ctx, self->on_leave);
    duk_push_heapptr (ctx, jic->object);
    duk_push_heapptr (ctx, retval->object);
    _gum_duk_scope_call_method (&scope, 1);
    duk_pop (ctx);

    gum_duk_invocation_return_value_reset (retval, NULL);
    gum_duk_interceptor_release_invocation_return_value (module, retval);

    _gum_duk_invocation_context_reset (jic, NULL);
    _gum_duk_interceptor_release_invocation_context (module, jic);

    _gum_duk_scope_leave (&scope);
  }
Пример #11
0
void
_gum_duk_push_uint64 (duk_context * ctx,
                      guint64 value,
                      GumDukCore * core)
{
  duk_push_heapptr (ctx, core->uint64);
  duk_push_pointer (ctx, &value);
  duk_new (ctx, 1);
}
Пример #12
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;
}
Пример #13
0
void
_gum_duk_push_native_resource (duk_context * ctx,
                               gpointer data,
                               GDestroyNotify notify,
                               GumDukCore * core)
{
  duk_push_heapptr (ctx, core->native_resource);
  duk_push_pointer (ctx, data);
  duk_push_pointer (ctx, GUM_FUNCPTR_TO_POINTER (notify));
  duk_new (ctx, 2);
}
Пример #14
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);
}
Пример #15
0
static int _worker_dispatch_cb (evHandle *handle){
    comoWorker *worker = handle->data;
    duk_context *ctx = worker->Mainctx;

    mtx_lock(&worker->mtx);
    QUEUE *q;
    while ( !QUEUE_EMPTY(&worker->queueOut) ){

        q = QUEUE_HEAD(&(worker)->queueOut);
        QUEUE_REMOVE(q);
        comoQueue *queue = QUEUE_DATA(q, comoQueue, queue);

        if (worker->destroy != 0){
            goto FREE;
        }

        duk_push_heapptr(ctx, worker->self);
        
        if (duk_get_type(ctx, -1) != DUK_TYPE_OBJECT){
            dump_stack(ctx, "DUK");
            assert(0);
        }

        como_push_worker_value(ctx, queue);

        duk_call(ctx, 1);
        duk_pop(ctx);

        FREE :
        /* free except in case of pointers */
        if (queue->data != NULL && queue->type != DUK_TYPE_POINTER){
            free(queue->data);
        }

        free(queue);
    }
    mtx_unlock(&worker->mtx);

    if (worker->destroy == 2){
        
        duk_push_global_stash(ctx);
        duk_get_prop_string(ctx, -1, "comoWorkersCallBack");
        duk_push_number(ctx, (double) handle->id);
        duk_del_prop(ctx, -2);

        handle_close(handle);
        free(worker);
    }

    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;
}
Пример #17
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;
}
Пример #18
0
static void
gum_kernel_scan_context_run (GumKernelScanContext * self)
{
  GumDukCore * core = self->core;
  GumDukScope script_scope;
  duk_context * ctx;

  gum_kernel_scan (&self->range, self->pattern,
      (GumMemoryScanMatchFunc) gum_kernel_scan_context_emit_match, self);

  ctx = _gum_duk_scope_enter (&script_scope, core);

  duk_push_heapptr (ctx, self->on_complete);
  _gum_duk_scope_call (&script_scope, 0);
  duk_pop (ctx);

  _gum_duk_scope_leave (&script_scope);
}
Пример #19
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;
  }
}
Пример #20
0
void
_gum_duk_protect (duk_context * ctx,
                  GumDukHeapPtr object)
{
  gchar name[32];
  duk_uint_t ref_count;

  if (object == NULL)
    return;

  sprintf (name, "protected_%p", object);

  duk_push_global_stash (ctx);

  duk_get_prop_string (ctx, -1, name);
  if (duk_is_undefined (ctx, -1))
  {
    duk_pop (ctx);

    duk_push_object (ctx);
    duk_push_heapptr (ctx, object);
    duk_put_prop_string (ctx, -2, "o");
    ref_count = 1;
    duk_push_uint (ctx, ref_count);
    duk_put_prop_string (ctx, -2, "n");

    duk_put_prop_string (ctx, -2, name);
  }
  else
  {
    duk_get_prop_string (ctx, -1, "n");
    ref_count = duk_get_uint (ctx, -1);
    duk_pop (ctx);
    ref_count++;
    duk_push_uint (ctx, ref_count);
    duk_put_prop_string (ctx, -2, "n");

    duk_pop (ctx);
  }

  duk_pop (ctx);
}
Пример #21
0
JavaScriptObject::~JavaScriptObject() {
    if (!m_instance) {
        // Instance has already been cleaned up.
        return;
    }
    // The instance still exists - detach from it.
    duk_push_global_object(m_context);
    duk_push_heapptr(m_context, m_instance);

    // Remove this pointer from the JS object's property.
    if (duk_get_prop_string(m_context, -1, WRAPPER_THIS_PROP_NAME)) {
        const duk_size_t length = duk_get_length(m_context, -1);
        for (duk_uarridx_t i = 0; i < length; ++i) {
            duk_get_prop_index(m_context, -1, i);

            const void* ptr = duk_get_pointer(m_context, -1);
            duk_pop(m_context);

            if (this == ptr) {
                // Remove this object from the array.
                duk_del_prop_index(m_context, -1, i);
                break;
            }
        }
    }

    // Pop the array (or undefined if there was none).
    duk_pop(m_context);

    if (m_nextFinalizer) {
        // Reset to the object's previous finalizer.
        duk_push_c_function(m_context, m_nextFinalizer, 1);
        duk_set_finalizer(m_context, -2);
    }

    // Pop the instance & global object.
    duk_pop_2(m_context);
}
Пример #22
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;
  }
}
Пример #23
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;
}
Пример #24
0
void
_gum_duk_add_properties_to_class_by_heapptr (
    duk_context * ctx,
    GumDukHeapPtr klass,
    const GumDukPropertyEntry * entries)
{
  const GumDukPropertyEntry * entry;

  duk_push_heapptr (ctx, klass);

  for (entry = entries; entry->name != NULL; entry++)
  {
    int idx = 1;
    int flags = DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE;

    duk_push_string (ctx, entry->name);
    idx++;

    if (entry->getter != NULL)
    {
      idx++;
      flags |= DUK_DEFPROP_HAVE_GETTER;
      duk_push_c_function (ctx, entry->getter, 0);
    }

    if (entry->setter != NULL)
    {
      idx++;
      flags |= DUK_DEFPROP_HAVE_SETTER;
      duk_push_c_function (ctx, entry->setter, 1);
    }

    duk_def_prop (ctx, -idx, flags);
  }

  duk_pop (ctx);
}
Пример #25
0
int GlobalStash::AddObject(void *ptr) {
    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, name_);
    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_heapptr(ctx, ptr);                                   // <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);                                                  //

    return freeSlot;
}
Пример #26
0
static gboolean
gum_emit_module_range (const GumKernelModuleRangeDetails * details,
                       GumDukMatchContext * mc)
{
  GumDukScope * scope = mc->scope;
  duk_context * ctx = scope->ctx;
  gboolean proceed = TRUE;

  duk_push_heapptr (ctx, mc->on_match);

  duk_push_object (ctx);

  duk_push_string (ctx, details->name);
  duk_put_prop_string (ctx, -2, "name");

  _gum_duk_push_uint64 (ctx, details->address, scope->core);
  duk_put_prop_string (ctx, -2, "base");

  duk_push_uint (ctx, details->size);
  duk_put_prop_string (ctx, -2, "size");

  _gum_duk_push_page_protection (ctx, details->protection);
  duk_put_prop_string (ctx, -2, "protection");

  if (_gum_duk_scope_call_sync (scope, 1))
  {
    if (duk_is_string (ctx, -1))
      proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0;
  }
  else
  {
    proceed = FALSE;
  }
  duk_pop (ctx);

  return proceed;
}
Пример #27
0
static gboolean
gum_emit_thread (const GumThreadDetails * details,
                 gpointer user_data)
{
  GumDukMatchContext * mc = user_data;
  GumDukScope * scope = mc->scope;
  duk_context * ctx = scope->ctx;
  gboolean proceed = TRUE;

  if (gum_script_backend_is_ignoring (details->id))
    return TRUE;

  duk_push_heapptr (ctx, mc->on_match);

  duk_push_object (ctx);
  duk_push_uint (ctx, details->id);
  duk_put_prop_string (ctx, -2, "id");
  duk_push_string (ctx, _gum_duk_thread_state_to_string (details->state));
  duk_put_prop_string (ctx, -2, "state");
  _gum_duk_push_cpu_context (ctx, (GumCpuContext *) &details->cpu_context,
      GUM_CPU_CONTEXT_READONLY, scope->core);
  duk_put_prop_string (ctx, -2, "context");

  if (_gum_duk_scope_call_sync (scope, 1))
  {
    if (duk_is_string (ctx, -1))
      proceed = strcmp (duk_require_string (ctx, -1), "stop") != 0;
  }
  else
  {
    proceed = FALSE;
  }
  duk_pop (ctx);

  return proceed;
}
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;
}
Пример #29
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;
}
Пример #30
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);
  }
}