duktape
duktape copied to clipboard
Verify Duktape/C code [question]
Hi,
I'm trying to validate that my approach is correct. It seems to work, but whenever I dump the stack I cant see the function "read". What I want to achieve is the following:
function DigitalIn(pin,mode) {
return { pin: pin,
mode: mode,
read: function(){
return true;
}
}
};
Main C code:
void main(void){
duk_push_c_function(ctx, native_digital_in, 2 /*nargs*/);
duk_put_global_string(ctx, "DigitalIn");
duk_eval_string_noresult(ctx, "
var din = DigitalIn(23,0);
var din2 = DigitalIn(2,1);
print(din.read());
print(din2.read());
");
static duk_ret_t read(duk_context *ctx) {
duk_push_this(ctx);
(void) duk_get_prop_string(ctx,0, "pin");
printf("obj.propertyName = %s\r\n", duk_to_string(ctx, -1));
(void) duk_get_prop_string(ctx, 0, "mode");
printf("obj.propertyName = %s\r\n", duk_to_string(ctx, -1));
duk_push_true(ctx);
return 1;
}
duk_ret_t native_digital_in(duk_context *ctx) {
uint32_t pin = duk_require_uint(ctx, 0);
uint32_t mode = duk_require_uint(ctx, 1);
duk_set_top(ctx, 0); // clear stack args
duk_idx_t obj_idx = duk_push_object(ctx); /* -> [ ... global obj ] */
duk_push_number(ctx, pin);
duk_put_prop_string(ctx, obj_idx, "pin");
duk_push_number(ctx, mode);
duk_put_prop_string(ctx, obj_idx, "mode");
duk_push_c_function(ctx, read, 0 /*nargs*/);
duk_put_prop_string(ctx, obj_idx, "read");
return 1;
}
What does the test print, and what stack dump do you get?
Quick observation (unrelated to the issue):
- The
duk_set_top(ctx, 0)
innative_digital_in
is not necessary. You can just push an object and return 1, Duktape will take care of the stack unwind.
Here is the output:
That output seems like it is working?
- Each call prints the expected property values
- Each call returns
true
, because of the duk_push_true() + return 1 combination
What is missing?
Yes it's working but I'm wondering why I can't see the function in the stack dump.
On Tue., 25 Jun. 2019, 8:06 pm Sami Vaarala, [email protected] wrote:
That output seems like it is working?
- Each call prints the expected property values
- Each call returns true, because of the duk_push_true() + return 1 combination
What is missing?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/svaarala/duktape/issues/2124?email_source=notifications&email_token=AMMJ33DZFHSQFLXMUDZA7UTP4HUZXA5CNFSM4H3EZV62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYPXI6A#issuecomment-505377912, or mute the thread https://github.com/notifications/unsubscribe-auth/AMMJ33EUYLOCSRSOGOEQCITP4HUZXANCNFSM4H3EZV6Q .
Can you try what happens if you Duktape.enc('jx', obj);
for the same object?
Hi,
It didn't print anything so I enabled it with: DUK_USE_JX: true
Now it's visible!
Good to hear :)
Hi,
So far it's working well but when I integrate it to my setTimeout losing "scope" it calls the object constructor again. What's the best approach to stop this from happening? Singleton?
var count = 0;
var intervalId;
var dout = DigitalOut(4,0);
var val = false;
setTimeout(function cb() {
print('one shot timer, called after 10 seconds');
}, 10000);
intervalId = setInterval(function () {
print('interval', ++count);
val = !val;
dout.write(val);
if (count >= 25) {
clearInterval(intervalId);
}
}, 1000);
print("Hello from Javascript!");
I removed var from dout :)