Fix the goddamn indentation (shudder)

parent 7202fbbf
[*]
end_of_line = lf
insert_final_newline = true
[*.{c,h}]
indent_style = space
indent_size = 2
\ No newline at end of file
/*
* Very simple example program
*/
* Very simple example program
*/
#include <stdio.h>
#include <stdlib.h> // malloc
#include "duktape.h"
// @see [1]
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <io.h>
#include <fcntl.h>
#endif
#include "native_print.c"
......@@ -17,48 +17,46 @@
#include "register_globals.c"
int main(int argc, char *argv[]) {
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);
freopen(NULL, "wb", stdout);
// @see [1]
#ifdef _WIN32
_setmode(0, _O_BINARY);
_setmode(1, _O_BINARY);
#endif
duk_context *ctx = duk_create_heap_default();
register_globals(ctx);
ret = native_import(ctx, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
if (argc < 2) {
printf("Usage: dukboots file-to-eval.js\n");
printf("Yup. Pretty basic API at the moment.\n");
exit(0);
}
ret = duk_pcall(ctx, 0);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
// Re-open stdin and stdout in binary mode
freopen(NULL, "rb", stdin);
freopen(NULL, "wb", stdout);
// @see [1]
#ifdef _WIN32
_setmode(0, _O_BINARY);
_setmode(1, _O_BINARY);
#endif
duk_int_t ret = 0;
duk_context *ctx = duk_create_heap_default();
register_globals(ctx);
ret = native_import(ctx, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
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);
return ret;
duk_destroy_heap(ctx);
return ret;
}
/**
* Footnotes
*
* [1] These hacks are needed because Windows mutates files opened in text mode
* (which stdin and stdout are) by replacing all \n with \r\n. I want
* binary identical output regardless of the host environment dammit.
*/
* Footnotes
*
* [1] These hacks are needed because Windows mutates files opened in text mode
* (which stdin and stdout are) by replacing all \n with \r\n. I want
* binary identical output regardless of the host environment dammit.
*/
static duk_ret_t native_adder(duk_context *ctx) {
int i;
int n = duk_get_top(ctx); /* #args */
double res = 0.0;
int i;
int n = duk_get_top(ctx); /* #args */
double res = 0.0;
for (i = 0; i < n; i++) {
res += duk_to_number(ctx, i);
}
for (i = 0; i < n; i++) {
res += duk_to_number(ctx, i);
}
duk_push_number(ctx, res);
return 1; /* one return value */
duk_push_number(ctx, res);
return 1; /* one return value */
}
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");
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);
long fileSize = ftell(pFile);
rewind(pFile);
// Allocate a buffer the write size
buffer = (char*) malloc (sizeof(char) * fileSize);
char * 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);
size_t 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
// Create a function from the file buffer. Leaves it on the stack.
duk_int_t ret;
duk_push_string(ctx, filename);
ret = duk_pcompile_lstring_filename(ctx, DUK_COMPILE_FUNCTION + DUK_COMPILE_STRICT, buffer, bytesRead);
free(buffer);
return ret;
}
static duk_ret_t native_print(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
\ No newline at end of file
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
// 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
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");
}
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