PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Show/Show.cpp

http://github.com/mbebenita/Broadway
C++ | 204 lines | 151 code | 37 blank | 16 comment | 30 complexity | 5c9b074a39a13feda6b00c40f9f8f6ff MD5 | raw file
  1#include <stdio.h>
  2#include <stdlib.h>
  3#include <unistd.h>
  4#include <js/jsapi.h>
  5#include <SDL/SDL.h>
  6#include "Stage.h"
  7#include "Avc.h"
  8#include <unistd.h>
  9
 10/* The class of the global object. */
 11static JSClass global_class = {
 12    "global", JSCLASS_GLOBAL_FLAGS,
 13    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
 14    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
 15    JSCLASS_NO_OPTIONAL_MEMBERS
 16};
 17
 18/* The error reporter callback. */
 19void reportError(JSContext *cx, const char *message, JSErrorReport *report) {
 20	fprintf(stderr, "%s:%u:%s\n",
 21			report->filename ? report->filename : "<no filename>",
 22			(unsigned int) report->lineno, message);
 23}
 24
 25JSBool print(JSContext *cx, uintN argc, jsval *vp) {
 26	char *str;
 27	JSString *msg;
 28
 29	if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "/S", &msg))
 30		return JS_FALSE;
 31
 32	/* If called with no arguments, use the current time as the seed. */
 33	if (argc == 0)
 34		str = "";
 35	else
 36		str = JS_EncodeString(cx, msg);
 37
 38	printf("%s", str);
 39
 40	JS_SET_RVAL(cx, vp, JSVAL_VOID);
 41	/* return undefined */
 42	return JS_TRUE;
 43}
 44
 45JSBool println(JSContext *cx, uintN argc, jsval *vp) {
 46	char *str;
 47	JSString *msg;
 48
 49	if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "/S", &msg))
 50		return JS_FALSE;
 51
 52	/* If called with no arguments, use the current time as the seed. */
 53	if (argc == 0)
 54		str = "";
 55	else
 56		str = JS_EncodeString(cx, msg);
 57
 58	printf("%s\n", str);
 59
 60	JS_SET_RVAL(cx, vp, JSVAL_VOID);
 61	/* return undefined */
 62	return JS_TRUE;
 63}
 64
 65/**
 66 * Reads a binary file and returns its content as a JS ArrayBuffer.
 67 */
 68JSObject *readFile(JSContext *cx, char *name) {
 69	FILE *file = fopen(name, "rb");
 70	if (!file) {
 71		fprintf(stderr, "Unable to open file %s\n", name);
 72		return NULL;
 73	}
 74
 75	fseek(file, 0, SEEK_END);
 76	unsigned long bufferSize = ftell(file) + 1;
 77	fseek(file, 0, SEEK_SET);
 78
 79	char *buffer = (char *) JS_malloc(cx, bufferSize + sizeof(uint64));
 80	if (!buffer) {
 81		fprintf(stderr, "Memory error!\n");
 82		fclose(file);
 83		return NULL;
 84	}
 85
 86	fread((char *)buffer + sizeof(uint64), bufferSize, 1, file);
 87	((uint64 *)buffer)[0] = bufferSize;
 88
 89	fclose(file);
 90	return JS_CreateArrayBuffer(cx, buffer, bufferSize);
 91}
 92
 93JSBool read(JSContext *cx, uintN argc, jsval *vp) {
 94	char *str;
 95	JSString *msg;
 96
 97	if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &msg))
 98		return JS_FALSE;
 99	str = JS_EncodeString(cx, msg);
100
101	JSObject *buffer = buffer = readFile(cx, str);
102
103	if (buffer != NULL) {
104		JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(buffer));
105	} else {
106		JS_SET_RVAL(cx, vp, JSVAL_VOID);
107		return JS_FALSE;
108	}
109	return JS_TRUE;
110}
111
112static JSFunctionSpec globalFunctions[] = {
113    JS_FS("print",   print,   0, 0),
114    JS_FS("println",   println,   0, 0),
115    JS_FS("read",   read,   1, 0),
116    JS_FS_END
117};
118
119
120
121int SDL_main(int argc, char **argv) {
122
123    bool referenceMode = false;
124    bool javaScriptMode = false;
125    char* scripts [32];
126    int scriptCount = 0;
127    char* args;
128
129    int o = 0;
130    while ((o = getopt (argc, argv, "rjs:a:")) != -1) {
131        switch (o) {
132        case 'r':
133            referenceMode = true;
134            break;
135        case 'j':
136            javaScriptMode = true;
137            break;
138        case 's':
139            scripts[scriptCount++] = optarg;
140            break;
141        case 'a':
142            args = optarg;
143            break;
144        case '?':
145            printf("[-r] [-s FILE] [-a ARGUMENTS]\n");
146            break;
147        }
148    }
149
150	if (referenceMode) {
151        Avc avc(args);
152        avc.Play();
153	} else if (javaScriptMode) {
154        /* JS variables. */
155        JSRuntime *rt;
156        JSContext *cx;
157        JSObject *global;
158
159        /* Create a JS runtime. */
160        rt = JS_NewRuntime(8L * 1024L * 1024L);
161        if (rt == NULL)
162            return 1;
163
164        /* Create a context. */
165        cx = JS_NewContext(rt, 8192);
166        if (cx == NULL)
167            return 1;
168        JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
169        JS_SetVersion(cx, JSVERSION_LATEST);
170        JS_SetErrorReporter(cx, reportError);
171
172        /* Create the global object in a new compartment. */
173        global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
174        if (global == NULL)
175            return 1;
176
177        /* Populate the global object with the standard globals,
178         like Object and Array. */
179        if (!JS_InitStandardClasses(cx, global))
180            return 1;
181
182        if (!JS_DefineFunctions(cx, global, globalFunctions))
183            return JS_FALSE;
184
185        for (int i = 0; i < scriptCount; i++) {
186            JSScript *script = JS_CompileFile(cx, global, scripts[i]);
187            if (script == NULL)
188                return 1;
189
190            jsval rval;
191            JSBool result = JS_ExecuteScript(cx, global, script, &rval);
192            if (!result) {
193                printf("Cannot execute script %s", scripts[i]);
194            }
195        }
196
197        /* Cleanup. */
198        JS_DestroyContext(cx);
199        JS_DestroyRuntime(rt);
200        JS_ShutDown();
201	}
202	return 0;
203}
204