Browse Source

Add duk_pnew() API call

pull/297/head
Sami Vaarala 9 years ago
parent
commit
0de7188910
  1. 28
      src/duk_api_call.c
  2. 1
      src/duk_api_public.h.in

28
src/duk_api_call.c

@ -406,6 +406,34 @@ DUK_EXTERNAL void duk_new(duk_context *ctx, duk_idx_t nargs) {
DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, DUK_STR_NOT_CONSTRUCTABLE);
}
DUK_LOCAL duk_ret_t duk__pnew_helper(duk_context *ctx) {
duk_uint_t nargs;
nargs = duk_to_uint(ctx, -1);
duk_pop(ctx);
duk_new(ctx, nargs);
return 1;
}
DUK_EXTERNAL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs) {
duk_int_t rc;
DUK_ASSERT_CTX_VALID(ctx);
/* For now, just use duk_safe_call() to wrap duk_new(). We can't
* simply use a protected duk_handle_call() because there's post
* processing which might throw. It should be possible to ensure
* the post processing never throws (except in internal errors and
* out of memory etc which are always allowed) and then remove this
* wrapper.
*/
duk_push_uint(ctx, nargs);
rc = duk_safe_call(ctx, duk__pnew_helper, nargs + 2 /*nargs*/, 1 /*nrets*/);
return rc;
}
DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_activation *act;

1
src/duk_api_public.h.in

@ -709,6 +709,7 @@ DUK_EXTERNAL_DECL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs);
DUK_EXTERNAL_DECL duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs);
DUK_EXTERNAL_DECL duk_int_t duk_pcall_prop(duk_context *ctx, duk_idx_t obj_index, duk_idx_t nargs);
DUK_EXTERNAL_DECL void duk_new(duk_context *ctx, duk_idx_t nargs);
DUK_EXTERNAL_DECL duk_int_t duk_pnew(duk_context *ctx, duk_idx_t nargs);
DUK_EXTERNAL_DECL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets);
/*

Loading…
Cancel
Save