Treat .js files as top-level, strict mode, functions.

parent b56a367e
print('More hello world!');
print('2+3=' + adder(2, 3));
\ No newline at end of file
function () {
print('More hello world!');
print('2+3=' + adder(2, 3));
}
\ No newline at end of file
......@@ -13,20 +13,17 @@
#include "native_print.c"
#include "native_adder.c"
void register_globals(duk_context *ctx) {
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");
duk_push_c_function(ctx, native_adder, DUK_VARARGS);
duk_put_global_string(ctx, "adder");
}
#include "native_import.c"
#include "register_globals.c"
int main(int argc, char *argv[]) {
FILE * pFile;
long fileSize;
char * buffer;
size_t bytesRead;
duk_int_t ret = 0;
if (argc < 2) {
printf("Usage: dukboots file-to-eval.js\n");
printf("Yup. Pretty basic API at the moment.\n");
exit(0);
}
// Re-open stdin and stdout in binary mode
freopen(NULL, "rb", stdin);
......@@ -40,45 +37,22 @@ int main(int argc, char *argv[]) {
duk_context *ctx = duk_create_heap_default();
register_globals(ctx);
if (argc > 1) {
// Open the file named in the first CLI argument
pFile = fopen(argv[1], "rb");
if (pFile == NULL) {
fputs("File error\n", stderr);
exit(1);
}
// Get file size http://www.cplusplus.com/reference/cstdio/fread/
fseek(pFile, 0, SEEK_END);
fileSize = ftell(pFile);
rewind(pFile);
// Allocate a buffer the write size
buffer = (char*) malloc (sizeof(char) * fileSize);
if (buffer == NULL) {
fputs("Memory error\n", stderr);
exit(2);
}
// Dump file into memory
bytesRead = fread(buffer, 1, fileSize, pFile);
if (bytesRead != fileSize) {
fputs("Reading error\n", stderr);
exit(3);
}
// Close the file bc we're done with it
fclose(pFile);
//printf("Loaded %ld bytes from '%s'\n", fileSize, argv[1]);
} else {
printf("Usage: dukboots file-to-eval.js\n");
printf("Yup. Pretty basic API at the moment.\n");
exit(0);
}
ret = native_import(ctx, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
duk_eval_lstring(ctx, buffer, bytesRead);
ret = duk_pcall(ctx, 0);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
finally:
duk_destroy_heap(ctx);
if (argc > 1) {
free(buffer);
}
return 0;
return ret;
}
/**
......
int native_import (duk_context *ctx, char * filename) {
FILE * pFile;
long fileSize;
char * buffer;
size_t bytesRead;
duk_int_t ret;
// Open the file
pFile = fopen(filename, "rb");
if (pFile == NULL) {
fputs("File error\n", stderr);
exit(1);
}
// Get file size http://www.cplusplus.com/reference/cstdio/fread/
fseek(pFile, 0, SEEK_END);
fileSize = ftell(pFile);
rewind(pFile);
// Allocate a buffer the write size
buffer = (char*) malloc (sizeof(char) * fileSize);
if (buffer == NULL) {
fputs("Memory error\n", stderr);
exit(2);
}
// Dump file into memory
bytesRead = fread(buffer, 1, fileSize, pFile);
if (bytesRead != fileSize) {
fputs("Reading error\n", stderr);
exit(3);
}
// Close the file bc we're done with it
fclose(pFile);
//printf("Loaded %ld bytes from '%s'\n", fileSize, argv[1]);
// Create a function closure from the file buffer.
// Leaves it on the stack.
duk_push_string(ctx, filename);
ret = duk_pcompile_lstring_filename(ctx, DUK_COMPILE_FUNCTION + DUK_COMPILE_STRICT, buffer, bytesRead);
free(buffer);
return ret;
}
\ No newline at end of file
// TODO: Can we remove this stuff once we have a global proxy object
// we pass into the top-level function?
void register_globals(duk_context *ctx) {
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");
duk_push_c_function(ctx, native_adder, DUK_VARARGS);
duk_put_global_string(ctx, "adder");
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment