Ejemplo n.º 1
0
AJ_Status AJS_UnmarshalPropArgs(duk_context* ctx, AJ_Message* msg, uint8_t accessor, duk_idx_t msgIdx)
{
    AJ_Status status;
    const char* iface;
    const char* prop;
    const char* signature;
    uint32_t propId;
    uint8_t secure = FALSE;

    AJ_InfoPrintf(("PushPropArgs\n"));

    if (accessor == AJ_PROP_GET_ALL) {
        status = AJ_UnmarshalArgs(msg, "s", &iface);
        if (status == AJ_OK) {
            duk_push_string(ctx, iface);
            /*
             * Save interface (read-only) so we know how to marshal the reply values
             */
            duk_push_string(ctx, AJS_HIDDEN_PROP("propIface"));
            duk_dup(ctx, -2);
            duk_def_prop(ctx, msgIdx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
        }
        /*
         * This call always returns an error status because the interface name we are passing in is
         * invalid but it will indicate if security is required so we can perform the check below
         */
        AJ_IdentifyProperty(msg, iface, "", &propId, &signature, &secure);
    } else {
        status = AJ_UnmarshalArgs(msg, "ss", &iface, &prop);
        if (status == AJ_OK) {
            status = AJ_IdentifyProperty(msg, iface, prop, &propId, &signature, &secure);
            if (status == AJ_OK) {
                duk_push_string(ctx, iface);
                duk_push_string(ctx, prop);
                if (accessor == AJ_PROP_GET) {
                    /*
                     * If we are getting a property save the signature so we know how to marshal the
                     * value in the reply.
                     */
                    duk_push_string(ctx, AJS_HIDDEN_PROP("propSig"));
                    duk_push_string(ctx, signature);
                    duk_def_prop(ctx, msgIdx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
                } else {
                    /*
                     * Push the value to set
                     */
                    status = PushArg(ctx, msg);
                }
            }
        }
    }
    /*
     * If the interface is secure check the message is encrypted
     */
    if ((status == AJ_OK) && secure && !(msg->hdr->flags & AJ_FLAG_ENCRYPTED)) {
        status = AJ_ERR_SECURITY;
        AJ_WarnPrintf(("Security violation accessing property\n"));
    }
    return status;
}
Ejemplo n.º 2
0
/*
 * call-seq:
 *   ctx_define_function(name, &block) -> nil
 *
 * Define a function defined in the global scope and identified by a name.
 *
 *     ctx.ctx_define_function("hello_world") { |ctx| 'Hello world' } #=> nil
 *
 */
static VALUE ctx_define_function(VALUE self, VALUE prop)
{
  VALUE block;
  struct state *state;
  duk_context *ctx;

  // a block is required
  if (!rb_block_given_p())
    rb_raise(rb_eArgError, "Expected block");

  // get the context
  Data_Get_Struct(self, struct state, state);
  ctx = state->ctx;

  // the c function is available in the global scope
  duk_push_global_object(ctx);

  duk_push_c_function(ctx, ctx_call_pushed_function, DUK_VARARGS);

  block = rb_block_proc();
  rb_ary_push(state->blocks, block); // block will be properly garbage collected

  // both block and state are required by the pushed function
  duk_push_string(ctx, "block");
  duk_push_pointer(ctx, (void *) block);
  duk_def_prop(ctx, -3,  DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0);

  duk_push_string(ctx, "state");
  duk_push_pointer(ctx, (void *) state);
  duk_def_prop(ctx, -3,  DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0);

  duk_put_prop_string(ctx, -2, StringValueCStr(prop));

  return Qnil;
}
Ejemplo n.º 3
0
duk_ret_t dukky_html_menu_element___proto(duk_context *ctx)
{
	/* Set this prototype's prototype (left-parent) */
	/* get prototype */
	duk_get_global_string(ctx, dukky_magic_string_prototypes);
	duk_get_prop_string(ctx, -1, "\xFF\xFFNETSURF_DUKTAPE_PROTOTYPE_HTMLELEMENT");
	duk_replace(ctx, -2);
	duk_set_prototype(ctx, 0);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "type");
	duk_push_c_function(ctx, dukky_html_menu_element_type_getter, 0);
	duk_push_c_function(ctx, dukky_html_menu_element_type_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "label");
	duk_push_c_function(ctx, dukky_html_menu_element_label_getter, 0);
	duk_push_c_function(ctx, dukky_html_menu_element_label_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "compact");
	duk_push_c_function(ctx, dukky_html_menu_element_compact_getter, 0);
	duk_push_c_function(ctx, dukky_html_menu_element_compact_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Set the destructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_html_menu_element___destructor, 1);
	duk_set_finalizer(ctx, -2);
	duk_pop(ctx);

	/* Set the constructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_html_menu_element___constructor, 2);
	duk_put_prop_string(ctx, -2, "\xFF\xFFNETSURF_DUKTAPE_INIT");
	duk_pop(ctx);

	return 1; /* The prototype object */
}
Ejemplo n.º 4
0
void DefineProperty(duk_context* ctx, const char* propertyName, duk_c_function getFunc, duk_c_function setFunc)
{
    duk_push_string(ctx, propertyName);
    if (setFunc)
    {
        duk_push_c_function(ctx, getFunc, 0);
        duk_push_c_function(ctx, setFunc, 1);
        duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER);
    }
    else
    {
        duk_push_c_function(ctx, getFunc, 0);
        duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
    }
}
Ejemplo n.º 5
0
duk_ret_t dukky_svg_element___proto(duk_context *ctx)
{
	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "style");
	duk_push_c_function(ctx, dukky_svg_element_style_getter, 0);
	duk_push_c_function(ctx, dukky_svg_element_style_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Set the destructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_svg_element___destructor, 1);
	duk_set_finalizer(ctx, -2);
	duk_pop(ctx);

	/* Set the constructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_svg_element___constructor, 1);
	duk_put_prop_string(ctx, -2, "\xFF\xFFNETSURF_DUKTAPE_INIT");
	duk_pop(ctx);

	return 1; /* The prototype object */
}
Ejemplo n.º 6
0
void
duk_push_sphere_bytearray(duk_context* ctx, bytearray_t* array)
{
	duk_push_object(ctx);
	duk_push_string(ctx, "bytearray"); duk_put_prop_string(ctx, -2, "\xFF" "sphere_type");
	duk_push_pointer(ctx, array); duk_put_prop_string(ctx, -2, "\xFF" "ptr");
	duk_push_c_function(ctx, js_ByteArray_finalize, DUK_VARARGS); duk_set_finalizer(ctx, -2);
	duk_push_c_function(ctx, js_ByteArray_toString, DUK_VARARGS); duk_put_prop_string(ctx, -2, "toString");
	duk_push_c_function(ctx, js_ByteArray_concat, DUK_VARARGS); duk_put_prop_string(ctx, -2, "concat");
	duk_push_c_function(ctx, js_ByteArray_slice, DUK_VARARGS); duk_put_prop_string(ctx, -2, "slice");
	duk_push_string(ctx, "length"); duk_push_int(ctx, array->size);
	duk_def_prop(ctx, -3,
		DUK_DEFPROP_HAVE_CONFIGURABLE | 0
		| DUK_DEFPROP_HAVE_WRITABLE | 0
		| DUK_DEFPROP_HAVE_VALUE);

	// return proxy object so we can catch array accesses
	duk_push_global_object(ctx);
	duk_get_prop_string(ctx, -1, "Proxy");
	duk_dup(ctx, -3);
	duk_push_object(ctx);
	duk_push_c_function(ctx, js_ByteArray_getProp, DUK_VARARGS); duk_put_prop_string(ctx, -2, "get");
	duk_push_c_function(ctx, js_ByteArray_setProp, DUK_VARARGS); duk_put_prop_string(ctx, -2, "set");
	duk_new(ctx, 2);
	duk_remove(ctx, -2);
	duk_remove(ctx, -2);
}
Ejemplo n.º 7
0
static duk_ret_t test_1(duk_context *ctx, void *udata) {
	(void) udata;

	/* Lightfunc, must sanitize the address for the expect string. */
	duk_eval_string(ctx,
		"(function (v) {\n"
		"    print(String(v).replace(/light_[0-9a-fA-f_]+/, 'light_PTR'));\n"
		"})");
	duk_push_c_lightfunc(ctx, dummy_func, 0 /*nargs*/, 0 /*length*/, 0 /*magic*/);
	duk_call(ctx, 1);
	duk_pop(ctx);

	/* Function with a .name containing invalid characters.
	 *
	 * This is not currently handled very well: the .toString() output
	 * uses the name as is, which can make the output technically
	 * incorrect because it won't parse as a function.  However, the
	 * only way to create such functions is from C code so this is a
	 * minor issue.
	 */

	duk_push_c_function(ctx, dummy_func, 0 /*nargs*/);
	duk_push_string(ctx, "name");
	duk_push_string(ctx, "dummy {");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);
	printf("%s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
static duk_ret_t test_basic(duk_context *ctx, void *udata) {
	(void) udata;

	duk_push_c_function(ctx, dummy_func, 1);
	duk_push_string(ctx, "length");
	duk_push_int(ctx, 3);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE |
	                      DUK_DEFPROP_CLEAR_WRITABLE |
	                      DUK_DEFPROP_CLEAR_ENUMERABLE |
	                      DUK_DEFPROP_SET_CONFIGURABLE);

	printf("added own .length\n");

	duk_eval_string(ctx,
		"(function (fn) {\n"
		"    print(typeof fn);\n"
		"    var desc = Object.getOwnPropertyDescriptor(fn, 'length');\n"
		"    print(desc.writable);\n"
		"    print(desc.enumerable);\n"
		"    print(desc.configurable);\n"
		"    print(desc.value);\n"
		"})");
	duk_dup(ctx, -2);
	duk_call(ctx, 1);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Ejemplo n.º 9
0
void Context::registerProp(index_t idx,
                           const char* id,
                           Function getter,
                           Function setter)
{
    duk_push_string(m_handle, id);
    if (idx < 0)
        --idx;

    duk_uint_t flags = 0;

    if (getter) {
        flags |= DUK_DEFPROP_HAVE_GETTER;
        duk_push_c_function(m_handle, getter, 0);
        if (idx < 0)
            --idx;
    }

    if (setter) {
        flags |= DUK_DEFPROP_HAVE_SETTER;
        duk_push_c_function(m_handle, setter, 1);
        if (idx < 0)
            --idx;
    }

    duk_def_prop(m_handle, idx, flags);
}
Ejemplo n.º 10
0
duk_ret_t dukky_drag_event___proto(duk_context *ctx)
{
	/* Set this prototype's prototype (left-parent) */
	/* get prototype */
	duk_get_global_string(ctx, dukky_magic_string_prototypes);
	duk_get_prop_string(ctx, -1, "\xFF\xFFNETSURF_DUKTAPE_PROTOTYPE_MOUSEEVENT");
	duk_replace(ctx, -2);
	duk_set_prototype(ctx, 0);

	/* Add readonly property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "dataTransfer");
	duk_push_c_function(ctx, dukky_drag_event_dataTransfer_getter, 0);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Set the destructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_drag_event___destructor, 1);
	duk_set_finalizer(ctx, -2);
	duk_pop(ctx);

	/* Set the constructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_drag_event___constructor, 2);
	duk_put_prop_string(ctx, -2, "\xFF\xFFNETSURF_DUKTAPE_INIT");
	duk_pop(ctx);

	return 1; /* The prototype object */
}
Ejemplo n.º 11
0
static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) {
	duk_idx_t i, n;

	n = duk_get_top(ctx);

	duk_get_global_string(ctx, "console");
	duk_get_prop_string(ctx, -1, "format");

	for (i = 0; i < n; i++) {
		if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) {
			/* Slow path formatting. */
			duk_dup(ctx, -1);  /* console.format */
			duk_dup(ctx, i);
			duk_call(ctx, 1);
			duk_replace(ctx, i);  /* arg[i] = console.format(arg[i]); */
		}
	}

	duk_pop_2(ctx);

	duk_push_string(ctx, " ");
	duk_insert(ctx, 0);
	duk_join(ctx, n);

	if (error_name) {
		duk_push_error_object(ctx, DUK_ERR_ERROR, "%s", duk_require_string(ctx, -1));
		duk_push_string(ctx, "name");
		duk_push_string(ctx, error_name);
		duk_def_prop(ctx, -3, DUK_DEFPROP_FORCE | DUK_DEFPROP_HAVE_VALUE);  /* to get e.g. 'Trace: 1 2 3' */
		duk_get_prop_string(ctx, -1, "stack");
	}

	printf("%s\n", duk_to_string(ctx, -1));
	return 0;
}
Ejemplo n.º 12
0
DUK_LOCAL duk_ret_t duk__error_setter_helper(duk_context *ctx, duk_small_uint_t stridx_key) {
	/* Attempt to write 'stack', 'fileName', 'lineNumber' works as if
	 * user code called Object.defineProperty() to create an overriding
	 * own property.  This allows user code to overwrite .fileName etc
	 * intuitively as e.g. "err.fileName = 'dummy'" as one might expect.
	 * See https://github.com/svaarala/duktape/issues/387.
	 */

	DUK_ASSERT_TOP(ctx, 1);  /* fixed arg count: value */

	duk_push_this(ctx);
	duk_push_hstring_stridx(ctx, (duk_small_int_t) stridx_key);
	duk_dup_0(ctx);

	/* [ ... obj key value ] */

	DUK_DD(DUK_DDPRINT("error setter: %!T %!T %!T",
	                   duk_get_tval(ctx, -3), duk_get_tval(ctx, -2), duk_get_tval(ctx, -1)));

	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE |
	                      DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE |
	                      DUK_DEFPROP_HAVE_ENUMERABLE | /*not enumerable*/
	                      DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE);
	return 0;
}
Ejemplo n.º 13
0
void Engine::registerGlobal(const char* id,
                            Function getter, Function setter)
{
  ContextHandle handle = m_ctx.handle();

  duk_push_global_object(handle);
  duk_push_string(handle, id);

  duk_idx_t objidx = -2;
  duk_uint_t flags = 0;

  if (getter) {
    flags |= DUK_DEFPROP_HAVE_GETTER;
    duk_push_c_function(handle, getter, 0);
    --objidx;
  }

  if (setter) {
    flags |= DUK_DEFPROP_HAVE_SETTER;
    duk_push_c_function(handle, setter, 1);
    --objidx;
  }

  duk_def_prop(handle, objidx, flags);
}
Ejemplo n.º 14
0
duk_ret_t dukky_radio_node_list___proto(duk_context *ctx)
{
	/* Set this prototype's prototype (left-parent) */
	/* get prototype */
	duk_get_global_string(ctx, dukky_magic_string_prototypes);
	duk_get_prop_string(ctx, -1, "\xFF\xFFNETSURF_DUKTAPE_PROTOTYPE_NODELIST");
	duk_replace(ctx, -2);
	duk_set_prototype(ctx, 0);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "value");
	duk_push_c_function(ctx, dukky_radio_node_list_value_getter, 0);
	duk_push_c_function(ctx, dukky_radio_node_list_value_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Set the destructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_radio_node_list___destructor, 1);
	duk_set_finalizer(ctx, -2);
	duk_pop(ctx);

	/* Set the constructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_radio_node_list___constructor, 2);
	duk_put_prop_string(ctx, -2, "\xFF\xFFNETSURF_DUKTAPE_INIT");
	duk_pop(ctx);

	return 1; /* The prototype object */
}
Ejemplo n.º 15
0
static void duk__console_reg_vararg_func(duk_context *ctx, duk_c_function func, const char *name, duk_uint_t flags) {
	duk_push_c_function(ctx, func, DUK_VARARGS);
	duk_push_string(ctx, "name");
	duk_push_string(ctx, name);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);  /* Improve stacktraces by displaying function name */
	duk_set_magic(ctx, -1, (duk_int_t) flags);
	duk_put_prop_string(ctx, -2, name);
}
Ejemplo n.º 16
0
static duk_int_t duk__eval_module_source(duk_context *ctx, void *udata) {
#else
static duk_int_t duk__eval_module_source(duk_context *ctx) {
#endif
	/*
	 *  Stack: [ ... module source ]
	 */

#if DUK_VERSION >= 19999
	(void) udata;
#endif

	/* Wrap the module code in a function expression.  This is the simplest
	 * way to implement CommonJS closure semantics and matches the behavior of
	 * e.g. Node.js.
	 */
	duk_push_string(ctx, "(function(exports,require,module,__filename,__dirname){");
	duk_dup(ctx, -2);  /* source */
	duk_push_string(ctx, "})");
	duk_concat(ctx, 3);

	/* [ ... module source func_src ] */

	(void) duk_get_prop_string(ctx, -3, "filename");
	duk_compile(ctx, DUK_COMPILE_EVAL);
	duk_call(ctx, 0);

	/* [ ... module source func ] */

	/* Set name for the wrapper function. */
	duk_push_string(ctx, "name");
	duk_push_string(ctx, "main");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);

	/* call the function wrapper */
	(void) duk_get_prop_string(ctx, -3, "exports");   /* exports */
	(void) duk_get_prop_string(ctx, -4, "require");   /* require */
	duk_dup(ctx, -5);                                 /* module */
	(void) duk_get_prop_string(ctx, -6, "filename");  /* __filename */
	duk_push_undefined(ctx);                          /* __dirname */
	duk_call(ctx, 5);

	/* [ ... module source result(ignore) ] */

	/* module.loaded = true */
	duk_push_true(ctx);
	duk_put_prop_string(ctx, -4, "loaded");

	/* [ ... module source retval ] */

	duk_pop_2(ctx);

	/* [ ... module ] */

	return 1;
}
Ejemplo n.º 17
0
duk_idx_t AJS_UnmarshalMessage(duk_context* ctx, AJ_Message* msg, uint8_t accessor)
{
    duk_idx_t objIndex = duk_push_object(ctx);

    duk_push_string(ctx, msg->sender);
    duk_put_prop_string(ctx, objIndex, "sender");

    if ((msg->hdr->msgType == AJ_MSG_METHOD_CALL) || (msg->hdr->msgType == AJ_MSG_SIGNAL)) {
        duk_push_string(ctx, msg->member);
        duk_put_prop_string(ctx, objIndex, "member");
        duk_push_string(ctx, msg->iface);
        duk_put_prop_string(ctx, objIndex, "iface");
        duk_push_string(ctx, msg->objPath);
        duk_put_prop_string(ctx, objIndex, "path");
        /* If sender == unique name then true, otherwise false */
        duk_push_boolean(ctx, !abs(strcmp(msg->sender, AJS_GetBusAttachment()->uniqueName)));
        duk_put_prop_string(ctx, objIndex, "fromSelf");

        if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) {
            AJS_ReplyInternal* msgReply;
            /*
             * Private read-only information needed for composing the reply
             */
            duk_push_string(ctx, AJS_HIDDEN_PROP("reply"));
            msgReply = duk_push_fixed_buffer(ctx, sizeof(AJS_ReplyInternal));
            msgReply->msgId = msg->msgId;
            msgReply->flags = msg->hdr->flags;
            msgReply->serialNum = msg->hdr->serialNum;
            msgReply->sessionId = msg->sessionId;
            msgReply->accessor = accessor;
            duk_def_prop(ctx, objIndex, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
            /*
             * Register the reply functions
             */
            duk_push_c_lightfunc(ctx, AJS_MethodCallReply, DUK_VARARGS, 0, 0);
            duk_put_prop_string(ctx, objIndex, "reply");
            duk_push_c_lightfunc(ctx, AJS_MethodCallError, DUK_VARARGS, 0, 0);
            duk_put_prop_string(ctx, objIndex, "errorReply");
        }
    } else {
        int isError = (msg->hdr->msgType == AJ_MSG_ERROR);

        AJ_InfoPrintf(("Reply serial %d\n", msg->replySerial));
        duk_push_int(ctx, msg->replySerial);
        duk_put_prop_string(ctx, objIndex, "replySerial");
        if (isError) {
            duk_push_string(ctx, msg->error);
            duk_put_prop_string(ctx, objIndex, "error");
        }
        duk_push_boolean(ctx, isError);
        duk_put_prop_string(ctx, objIndex, "isErrorReply");
    }
    return objIndex;
}
Ejemplo n.º 18
0
/*
 * Create a new IO object
 */
static int NewIOObject(duk_context* ctx, void* pinCtx, int pinFunc, duk_c_function finalizer)
{
    int idx = duk_push_object(ctx);

    duk_push_string(ctx, "pin");
    duk_dup(ctx, 0);
    duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);

    duk_push_string(ctx, AJS_HIDDEN_PROP("ctx"));
    duk_push_pointer(ctx, pinCtx);
    duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);

    duk_push_string(ctx, "pinFunction");
    duk_push_int(ctx, pinFunc);
    duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
    /*
     * Callback from when the pin object is garbage collected.
     */
    AJS_RegisterFinalizer(ctx, idx, finalizer);
    return idx;
}
Ejemplo n.º 19
0
Archivo: vm.c Proyecto: joegen/sjs
static void sjs__setup_modsearch(sjs_vm_t* vm) {
    duk_context* ctx = vm->ctx;

    duk_get_global_string(ctx, "Duktape");
    duk_push_string(ctx, "modSearch");
    duk_push_c_function(ctx, sjs__modsearch, 4 /* nargs */);
    duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE |
                          DUK_DEFPROP_CLEAR_WRITABLE |
                          DUK_DEFPROP_CLEAR_ENUMERABLE |
                          DUK_DEFPROP_CLEAR_CONFIGURABLE);
    duk_pop(ctx);
}
Ejemplo n.º 20
0
static void test_one_flags(duk_context *ctx, duk_uint_t defprop_flags) {
	duk_eval_string(ctx, "({})");
	duk_push_string(ctx, "prop");
	duk_push_uint(ctx, 123);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE | defprop_flags);
	dump_prop(ctx);
	duk_pop(ctx);

	duk_eval_string(ctx, "(function () { var o = {}; Object.defineProperty(o, 'prop', {value:-123,writable:true,enumerable:true,configurable:true}); return o; })()");
	duk_push_string(ctx, "prop");
	duk_push_uint(ctx, 123);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE | defprop_flags);
	dump_prop(ctx);
	duk_pop(ctx);

	duk_eval_string(ctx, "(function () { var o = {}; Object.defineProperty(o, 'prop', {value:-123,writable:false,enumerable:false,configurable:false}); return o; })()");
	duk_push_string(ctx, "prop");
	duk_push_uint(ctx, 123);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE | defprop_flags);
	dump_prop(ctx);
	duk_pop(ctx);
}
Ejemplo n.º 21
0
DUK_INTERNAL duk_ret_t duk_bi_object_prototype_defineaccessor(duk_hthread *thr) {
	duk_push_this(thr);
	duk_insert(thr, 0);
	duk_to_object(thr, 0);
	duk_require_callable(thr, 2);

	/* [ ToObject(this) key getter/setter ] */

	/* ToPropertyKey() coercion is not needed, duk_def_prop() does it. */
	duk_def_prop(thr, 0, DUK_DEFPROP_SET_ENUMERABLE |
	                     DUK_DEFPROP_SET_CONFIGURABLE |
	                     (duk_get_current_magic(thr) ? DUK_DEFPROP_HAVE_SETTER : DUK_DEFPROP_HAVE_GETTER));
	return 0;
}
Ejemplo n.º 22
0
static void duk__push_require_function(duk_context *ctx, const char *id) {
	duk_push_c_function(ctx, duk__handle_require, 1);
	duk_push_string(ctx, "name");
	duk_push_string(ctx, "require");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE);
	duk_push_string(ctx, id);
	duk_put_prop_string(ctx, -2, "\xff" "moduleId");

	/* require.cache */
	duk_push_global_stash(ctx);
	(void) duk_get_prop_string(ctx, -1, "\xff" "requireCache");
	duk_put_prop_string(ctx, -3, "cache");
	duk_pop(ctx);
}
Ejemplo n.º 23
0
void StyleContext::addAccessor(const std::string& _name) {

    auto it = m_accessors.find(_name);
    if (it != m_accessors.end()) {
        return;
    }

    auto entry = m_accessors.emplace(_name, Accessor{_name, this});
    if (!entry.second) {
        return; // hmm, already added..
    }

    Accessor& attr = (*entry.first).second;

    // push 'feature' obj onto stack
    if (!duk_get_global_string(m_ctx, "feature")) {
        logMsg("Error: 'feature' not in global scope\n");
        return;
    }

    // push property name
    duk_push_string(m_ctx, _name.c_str());

    // push getter function
    duk_push_c_function(m_ctx, jsPropertyGetter, 0 /*nargs*/);
    duk_push_pointer(m_ctx, (void*)&attr);
    duk_put_prop_string(m_ctx, -2, ATTR_ID);

    // push setter function
    // duk_push_c_function(m_ctx, jsPropertySetter, 1 /*nargs*/);
    // duk_push_pointer(m_ctx, (void*)&attr);
    // duk_put_prop_string(m_ctx, -2, ATTR_ID);

    // stack: [ feature_obj, name, getter, setter ] -> [ feature_obj.name ]
    duk_def_prop(m_ctx, -3,
                 DUK_DEFPROP_HAVE_GETTER |
                 // DUK_DEFPROP_HAVE_SETTER |
                 // DUK_DEFPROP_WRITABLE |
                 // DUK_DEFPROP_HAVE_ENUMERABLE |
                 // DUK_DEFPROP_ENUMERABLE |
                 // DUK_DEFPROP_HAVE_CONFIGURABLE |
                 0);

    // pop feature obj
    duk_pop(m_ctx);

    DUMP("addAccessor\n");
}
Ejemplo n.º 24
0
AJ_Status AJS_RegisterIO(duk_context* ctx, duk_idx_t ioIdx)
{
    duk_idx_t pinIdx;
    duk_idx_t i;
    uint16_t numPins = AJS_TargetIO_GetNumPins();

    /*
     * Create base pin protoype
     */
    pinIdx = duk_push_object(ctx);
    AJS_SetPropertyAccessors(ctx, pinIdx, "functions", NULL, NativeFunctionsGetter);
    AJS_SetPropertyAccessors(ctx, pinIdx, "info", NULL, NativeInfoGetter);
    /*
     * Create the pin objects
     */
    duk_push_array(ctx);
    for (i = 0; i < numPins; ++i) {
        AJS_CreateObjectFromPrototype(ctx, pinIdx);
        duk_push_int(ctx, i);
        duk_put_prop_string(ctx, -2, "id");
        duk_put_prop_index(ctx, -2, i);
    }
    duk_put_prop_string(ctx, ioIdx, "pin");
    duk_pop(ctx);
    /*
     * Register the native functions
     */
    AJS_PutFunctionList(ctx, ioIdx, io_native_functions, TRUE);
    /*
     * GPIO attribute constants
     */
    duk_put_number_list(ctx, ioIdx, io_constants);
    /*
     * A hidden property for keeping track of triggers
     */
    duk_push_string(ctx, AJS_HIDDEN_PROP("trigs"));
    duk_push_array(ctx);
    duk_def_prop(ctx, ioIdx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);

    /*
     * Compact the IO object and set it on the global object
     */
    duk_compact(ctx, ioIdx);
    duk_dup(ctx, ioIdx);
    duk_put_global_string(ctx, AJS_IOObjectName);
    return AJ_OK;
}
Ejemplo n.º 25
0
void duk_module_node_init(duk_context *ctx) { 
	/*
	 *  Stack: [ ... options ] => [ ... ]
	 */

	duk_idx_t options_idx;

	duk_require_object_coercible(ctx, -1);  /* error before setting up requireCache */
	options_idx = duk_require_normalize_index(ctx, -1);

	/* Initialize the require cache to a fresh object. */
	duk_push_global_stash(ctx);
	duk_push_bare_object(ctx);
	duk_put_prop_string(ctx, -2, "\xff" "requireCache");
	duk_pop(ctx);

	/* Stash callbacks for later use.  User code can overwrite them later
	 * on directly by accessing the global stash.
	 */
	duk_push_global_stash(ctx);
	duk_get_prop_string(ctx, options_idx, "resolve");
	duk_require_function(ctx, -1);
	duk_put_prop_string(ctx, -2, "\xff" "modResolve");
	duk_get_prop_string(ctx, options_idx, "load");
	duk_require_function(ctx, -1);
	duk_put_prop_string(ctx, -2, "\xff" "modLoad");
	duk_pop(ctx);

	/* Stash main module. */
	duk_push_global_stash(ctx);
	duk_push_undefined(ctx);
	duk_put_prop_string(ctx, -2, "\xff" "mainModule");
	duk_pop(ctx);

	/* register `require` as a global function. */
	duk_push_global_object(ctx);
	duk_push_string(ctx, "require"); 
	duk__push_require_function(ctx, "");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE |
	                      DUK_DEFPROP_SET_WRITABLE |
	                      DUK_DEFPROP_SET_CONFIGURABLE);
	duk_pop(ctx);

	duk_pop(ctx);  /* pop argument */
}
static duk_ret_t havefile2_helper_2(duk_context *ctx) {
	duk_push_string(ctx,
		"(function () {\n"
		"    var fn = function noFileName(v) { v(); return 123; };\n"
		"    print('delete: ' + delete fn.fileName);\n"
		"    return fn;\n"
		"})()");
	duk_push_string(ctx, "dummy_filename.js");
	duk_compile(ctx, DUK_COMPILE_EVAL);
	duk_call(ctx, 0);

	duk_push_c_function(ctx, target_func_hack, 0);
	duk_push_string(ctx, "fileName");
	duk_push_string(ctx, "dummy_filename.c");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_WRITABLE | DUK_DEFPROP_SET_CONFIGURABLE);
	duk_call(ctx, 1);
	return 0;
}
static duk_ret_t deep_helper(duk_context *ctx, duk_c_function target_func, int depth) {
	duk_int_t rc;

	target_func_hack = target_func;
	depth_hack = depth;
	duk_push_c_function(ctx, deep_helper_2, 0);  /* Duktape/C func with .fileName */
	duk_push_string(ctx, "fileName");
	duk_push_string(ctx, "outer_limits.c");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_WRITABLE | DUK_DEFPROP_SET_CONFIGURABLE);
	duk_pcall(ctx, 0);
	(void) rc;

	duk_eval_string(ctx, "(function (e) { print(e.fileName, e.lineNumber); if (PRINT_STACK) { print(e.stack); } })");
	duk_dup(ctx, -2);
	duk_call(ctx, 1);
	duk_pop(ctx);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Ejemplo n.º 28
0
void Engine::registerClass(const char* id,
                           Function ctorFunc, int ctorNargs,
                           const FunctionEntry* methods,
                           const PropertyEntry* props)
{
  ContextHandle handle = m_ctx.handle();
  duk_push_c_function(handle, ctorFunc, ctorNargs);

  duk_push_object(handle);

  if (methods)
    duk_put_function_list(handle, -1, (const duk_function_list_entry*)methods);

  if (props) {
    for (int i=0; props[i].key; ++i) {
      duk_push_string(handle, props[i].key);

      duk_idx_t objidx = -2;
      duk_uint_t flags = 0;

      if (props[i].getter) {
        flags |= DUK_DEFPROP_HAVE_GETTER;
        duk_push_c_function(handle, props[i].getter, 0);
        --objidx;
      }

      if (props[i].setter) {
        flags |= DUK_DEFPROP_HAVE_SETTER;
        duk_push_c_function(handle, props[i].setter, 1);
        --objidx;
      }

      duk_def_prop(handle, objidx, flags);
    }
  }

  duk_put_prop_string(handle, -2, "prototype");

  duk_put_global_string(handle, id);
}
Ejemplo n.º 29
0
static duk_ret_t test_1(duk_context *ctx, void *udata) {
	(void) udata;

	/* Check that Function.prototype.name is writable. */
	duk_eval_string_noresult(ctx,
		"var pd = Object.getOwnPropertyDescriptor(Function.prototype, 'name');\n"
		"print('writable:', pd.writable);\n"
		"print('enumerable:', pd.enumerable);\n"
		"print('configurable:', pd.configurable);\n");

	duk_push_c_function(ctx, my_func, 0);
	duk_push_string(ctx, "name");
	duk_push_string(ctx, "my_func_name");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE);
	duk_put_global_string(ctx, "MyFunc");

	duk_eval_string_noresult(ctx,
		"print('MyFunc.name:', MyFunc.name);\n");

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Ejemplo n.º 30
0
static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) {
	duk_uint_t flags = (duk_uint_t) duk_get_current_magic(ctx);
	FILE *output = (flags & DUK_CONSOLE_STDOUT_ONLY) ? stdout : stderr;
	duk_idx_t n = duk_get_top(ctx);
	duk_idx_t i;

	duk_get_global_string(ctx, "console");
	duk_get_prop_string(ctx, -1, "format");

	for (i = 0; i < n; i++) {
		if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) {
			/* Slow path formatting. */
			duk_dup(ctx, -1);  /* console.format */
			duk_dup(ctx, i);
			duk_call(ctx, 1);
			duk_replace(ctx, i);  /* arg[i] = console.format(arg[i]); */
		}
	}

	duk_pop_2(ctx);

	duk_push_string(ctx, " ");
	duk_insert(ctx, 0);
	duk_join(ctx, n);

	if (error_name) {
		duk_push_error_object(ctx, DUK_ERR_ERROR, "%s", duk_require_string(ctx, -1));
		duk_push_string(ctx, "name");
		duk_push_string(ctx, error_name);
		duk_def_prop(ctx, -3, DUK_DEFPROP_FORCE | DUK_DEFPROP_HAVE_VALUE);  /* to get e.g. 'Trace: 1 2 3' */
		duk_get_prop_string(ctx, -1, "stack");
	}

	fprintf(output, "%s\n", duk_to_string(ctx, -1));
	if (flags & DUK_CONSOLE_FLUSH) {
		fflush(output);
	}
	return 0;
}