Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading JS variable within C. #2518

Closed
uwejan opened this issue Dec 6, 2022 · 3 comments
Closed

Reading JS variable within C. #2518

uwejan opened this issue Dec 6, 2022 · 3 comments

Comments

@uwejan
Copy link

uwejan commented Dec 6, 2022

Say i already have an JSON object exported and included into duktap.
Used 'duk_peval_string_noresult(ctx, ...long string)' to include my object.
Question would be how to read a global JS variable within C?

Thanks in advance.

@svaarala
Copy link
Owner

svaarala commented Dec 6, 2022

If the value is already set to the global object, you can use:

@uwejan
Copy link
Author

uwejan commented Dec 6, 2022

Bare with me please, still new to duktape and C.
I do not see that returning a result.
Say my json object called my_obj
duk_bool_t duk_get_global_string(duk_context *ctx, "my_obj");

@uwejan
Copy link
Author

uwejan commented Dec 6, 2022

@svaarala This sequance is not working.

	ctx = duk_create_heap_default();

	//duk_json_decode(ctx, "my_db");
	//duk_get_string(ctx, 1);

	duk_get_global_string(ctx, "my_obj");
	duk_dup(ctx, 0);

	duk_call_method(ctx, 0);

	printf("JSON  %s\n", duk_to_string(ctx, 0));
	duk_destroy_heap(ctx);
	

The globale object is stringified, and is nested as well.

@uwejan uwejan closed this as completed Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants