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