From 0de7188910c8f63b238e8f068cd5e342d2e7e24f Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 10 Aug 2015 22:42:28 +0300 Subject: [PATCH] Add duk_pnew() API call --- src/duk_api_call.c | 28 ++++++++++++++++++++++++++++ src/duk_api_public.h.in | 1 + 2 files changed, 29 insertions(+) diff --git a/src/duk_api_call.c b/src/duk_api_call.c index 3a6859cb..38dfc0e8 100644 --- a/src/duk_api_call.c +++ b/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; diff --git a/src/duk_api_public.h.in b/src/duk_api_public.h.in index 5113f757..0528abe6 100644 --- a/src/duk_api_public.h.in +++ b/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); /*