From f3baa051956821f6b871ebd9f814961baeb8973b Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 27 Feb 2021 14:15:13 -0800 Subject: [PATCH] Define console.log() in hellojs.com example Fixes #69 --- examples/hello.js | 4 ++-- examples/hellojs.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/hello.js b/examples/hello.js index d5058ec81..1304a49e7 100644 --- a/examples/hello.js +++ b/examples/hello.js @@ -1,2 +1,2 @@ -NativePrint('Hello world!\n'); -NativePrint('2+3=' + NativeAdd(2, 3) + '\n'); +console.log('Hello world!\n'); +console.log('2+3=' + NativeAdd(2, 3) + '\n'); diff --git a/examples/hellojs.c b/examples/hellojs.c index fa8c5b67d..498daf267 100644 --- a/examples/hellojs.c +++ b/examples/hellojs.c @@ -42,11 +42,21 @@ int main(int argc, char *argv[]) { duk_context *ctx; showcrashreports(); ctx = duk_create_heap_default(); - duk_push_c_function(ctx, NativePrint, DUK_VARARGS); - duk_put_global_string(ctx, "NativePrint"); + + /* define NativeAdd() */ duk_push_c_function(ctx, NativeAdd, DUK_VARARGS); duk_put_global_string(ctx, "NativeAdd"); + /* define console.log() */ + duk_push_global_object(ctx); + duk_push_c_function(ctx, NativePrint, DUK_VARARGS); + duk_push_string(ctx, "name"); + duk_push_string(ctx, "log"); + duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); + duk_set_magic(ctx, -1, 0); + duk_put_prop_string(ctx, -2, "log"); + duk_put_global_string(ctx, "console"); + CHECK_NE(-1, (fd = open("zip:examples/hello.js", O_RDONLY))); CHECK_NE(-1, fstat(fd, &st)); CHECK_NOTNULL((code = gc(calloc(1, st.st_size + 1))));