Define console.log() in hellojs.com example

Fixes #69
This commit is contained in:
Justine Tunney 2021-02-27 14:15:13 -08:00
parent 1c9065510f
commit f3baa05195
2 changed files with 14 additions and 4 deletions

View file

@ -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');

View file

@ -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))));