JSSpiderMonkeyVM.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #ifdef USE_SPIDERMONKEY
  5. #include "AtomicEditor.h"
  6. #include "/Users/josh/Desktop/SpiderMonkey/include/mozjs-37a1/jsapi.h"
  7. #include "js/RootingAPI.h"
  8. #include "JSSpiderMonkeyVM.h"
  9. using namespace JS;
  10. #include "Context.h"
  11. #include "FileSystem.h"
  12. #include "ResourceCache.h"
  13. #include "JSFile.h"
  14. static RootedObject* __global = NULL;
  15. // The class of the global object.
  16. static JSClass globalClass = {
  17. "global",
  18. JSCLASS_GLOBAL_FLAGS,
  19. JS_PropertyStub,
  20. JS_DeletePropertyStub,
  21. JS_PropertyStub,
  22. JS_StrictPropertyStub,
  23. JS_EnumerateStub,
  24. JS_ResolveStub,
  25. JS_ConvertStub,
  26. nullptr, nullptr, nullptr, nullptr,
  27. JS_GlobalObjectTraceHook
  28. };
  29. // The error reporter callback.
  30. void reportError(JSContext *cx, const char *message, JSErrorReport *report) {
  31. fprintf(stderr, "%s:%u:%s\n",
  32. report->filename ? report->filename : "[no filename]",
  33. (unsigned int) report->lineno,
  34. message);
  35. }
  36. namespace AtomicEditor
  37. {
  38. WeakPtr<JSSpiderMonkeyVM> JSSpiderMonkeyVM::instance_;
  39. static bool
  40. print(JSContext *cx, unsigned argc, jsval *vp)
  41. {
  42. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  43. for (unsigned i = 0; i < args.length(); i++) {
  44. JSString *str = JS::ToString(cx, args[i]);
  45. if (!str)
  46. return false;
  47. char *bytes = JS_EncodeString(cx, str);
  48. if (!bytes)
  49. return false;
  50. printf("%s%s", i ? " " : "", bytes);
  51. JS_free(cx, bytes);
  52. }
  53. putchar('\n');
  54. fflush(stdout);
  55. args.rval().setUndefined();
  56. return true;
  57. }
  58. JSSpiderMonkeyVM::JSSpiderMonkeyVM(Context* context) :
  59. Object(context), runtime_(0), jscontext_(0),
  60. autorequest_(0), autocompartment_(0)
  61. {
  62. context_->RegisterSubsystem(this);
  63. instance_ = this;
  64. // Initialize the JS engine.
  65. if (!JS_Init())
  66. {
  67. assert(0);
  68. }
  69. // Create a JS runtime.
  70. runtime_ = JS_NewRuntime(32L * 1024L * 1024L);
  71. if (runtime_)
  72. {
  73. jscontext_ = JS_NewContext(runtime_, 8192);
  74. if (jscontext_)
  75. {
  76. JS_SetErrorReporter(runtime_, reportError);
  77. }
  78. else
  79. {
  80. assert(0);
  81. }
  82. }
  83. else
  84. {
  85. assert(0);
  86. }
  87. // Enter a request before running anything in the context.
  88. autorequest_ = new JSAutoRequest(jscontext_);
  89. // Create the global object and a new compartment.
  90. __global = new RootedObject(jscontext_);
  91. *__global = JS_NewGlobalObject(jscontext_, &globalClass, nullptr,
  92. JS::DontFireOnNewGlobalHook);
  93. // Enter the new global object's compartment.
  94. autocompartment_ = new JSAutoCompartment(jscontext_, *__global);
  95. // Populate the global object with the standard globals, like Object and
  96. // Array.
  97. if (!JS_InitStandardClasses(jscontext_, *__global))
  98. {
  99. assert(0);
  100. }
  101. if (!JS_InitReflect(jscontext_, *__global))
  102. {
  103. assert(0);
  104. }
  105. JS_DefineFunction(jscontext_, *__global, "print", (JSNative) print, 0, 0);
  106. ExecuteFile("AtomicEditor/javascript/modules/acorn_spidermonkey.js");
  107. }
  108. JSSpiderMonkeyVM::~JSSpiderMonkeyVM()
  109. {
  110. instance_ = NULL;
  111. if (autocompartment_)
  112. delete autocompartment_;
  113. if (__global)
  114. {
  115. delete __global;
  116. __global = NULL;
  117. }
  118. if (autorequest_)
  119. delete autorequest_;
  120. // Shut everything down.
  121. if (context_)
  122. JS_DestroyContext(jscontext_);
  123. if (runtime_)
  124. JS_DestroyRuntime(runtime_);
  125. JS_ShutDown();
  126. }
  127. bool JSSpiderMonkeyVM::ExecuteFile(const String& path)
  128. {
  129. SharedPtr<JSFile> jsfile;
  130. jsfile = GetSubsystem<ResourceCache>()->GetResource<JSFile>(path);
  131. JS::RootedValue returnValue(jscontext_);
  132. OwningCompileOptions options(jscontext_);
  133. options.setFileAndLine(jscontext_, path.CString(), 0);
  134. options.setCompileAndGo(true);
  135. bool ok = JS::Evaluate(jscontext_, *__global, options,
  136. jsfile->GetSource(), strlen(jsfile->GetSource()), &returnValue);
  137. return ok;
  138. }
  139. bool JSSpiderMonkeyVM::ParseJavascriptToJSON(const char* source, String& json)
  140. {
  141. json.Clear();
  142. bool ok = true;
  143. JS::Rooted<JS::Value> v(jscontext_);
  144. JS::RootedValue returnValue(jscontext_);
  145. JS::RootedValue jsource(jscontext_, JS::StringValue(JS_NewStringCopyZ(jscontext_, source)));
  146. JS_CallFunctionName(jscontext_, *__global, "__atomic_parse_to_json", HandleValueArray(jsource), &returnValue);
  147. JS::RootedString hm(jscontext_);
  148. hm = JS::ToString(jscontext_, returnValue);
  149. const char* strjson = JS_EncodeStringToUTF8(jscontext_, hm);
  150. json = strjson;
  151. //printf("%s\n", strjson);
  152. JS_free(jscontext_, (void *) strjson);
  153. return ok;
  154. }
  155. bool JSSpiderMonkeyVM::ParseJavascriptToJSONWithSpiderMonkeyReflectAPI(const char* source, String& json)
  156. {
  157. json.Clear();
  158. bool ok = true;
  159. JS::Rooted<JS::Value> v(jscontext_);
  160. JS::RootedValue returnValue(jscontext_);
  161. JS::RootedValue jsource(jscontext_, JS::StringValue(JS_NewStringCopyZ(jscontext_, source)));
  162. // get the Reflect object
  163. if (!JS_GetProperty(jscontext_, *__global, "Reflect", &v))
  164. return false;
  165. JS::RootedObject reflectObject(jscontext_);
  166. JS_ValueToObject(jscontext_, v, &reflectObject);
  167. JS_CallFunctionName(jscontext_, reflectObject, "parse", HandleValueArray(jsource), &returnValue);
  168. JS::RootedObject jsonObject(jscontext_);
  169. if (!JS_GetProperty(jscontext_, *__global, "JSON", &v))
  170. return false;
  171. JS_ValueToObject(jscontext_, v, &jsonObject);
  172. // pretty printed, can remove for speed
  173. JS::AutoValueArray<3> args(jscontext_);
  174. args[0].set(returnValue);
  175. args[1].set(JS::NullValue());
  176. args[2].setNumber(3.0);
  177. JS::RootedValue jjson(jscontext_);
  178. JS_CallFunctionName(jscontext_, jsonObject, "stringify", HandleValueArray(args), &jjson);
  179. JS::RootedString hm(jscontext_);
  180. hm = JS::ToString(jscontext_, jjson);
  181. const char* strjson = JS_EncodeStringToUTF8(jscontext_, hm);
  182. json = strjson;
  183. //printf("%s\n", strjson);
  184. JS_free(jscontext_, (void *) strjson);
  185. return ok;
  186. }
  187. bool JSSpiderMonkeyVM::ReadZeroTerminatedSourceFile(const String& path, String& source)
  188. {
  189. File file(instance_->GetContext());
  190. file.Open(path);
  191. unsigned size = file.GetSize();
  192. SharedArrayPtr<char> data;
  193. data = new char[size + 1];
  194. data[size] = '\0';
  195. file.Read(data, size);
  196. source = data;
  197. return true;
  198. }
  199. }
  200. int run(JSContext *cx) {
  201. // Enter a request before running anything in the context.
  202. JSAutoRequest ar(cx);
  203. // Create the global object and a new compartment.
  204. RootedObject global(cx);
  205. global = JS_NewGlobalObject(cx, &globalClass, nullptr,
  206. JS::DontFireOnNewGlobalHook);
  207. if (!global)
  208. return 1;
  209. // Enter the new global object's compartment.
  210. JSAutoCompartment ac(cx, global);
  211. // Populate the global object with the standard globals, like Object and
  212. // Array.
  213. if (!JS_InitStandardClasses(cx, global))
  214. return 1;
  215. // Your application code here. This may include JSAPI calls to create your
  216. // own custom JS objects and run scripts.
  217. return 0;
  218. }
  219. #endif