#include "stdafx.h" #include "rmgen.h" #include "map.h" #include "output.h" #include "api.h" #include "random.h" using namespace std; const char* LIBRARY_FILE = "library.js"; JSRuntime *rt = 0; JSContext *cx = 0; JSObject *global = 0; Map* theMap = 0; // JS support functions void ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report) { cerr << "Error at " << report->filename << ":" << report->lineno << ":\n\t" << message << endl; Shutdown(1); } void InitJS() { rt = JS_NewRuntime(8L * 1024L * 1024L); cx = JS_NewContext(rt, 8192); JS_SetErrorReporter(cx, ErrorReporter); static JSClass globalClass = { "global",0, JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub, JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub }; global = JS_NewObject(cx, &globalClass, NULL, NULL); JS_InitStandardClasses(cx, global); JS_DefineFunctions(cx, global, globalFunctions); } void Shutdown(int status) { JS_DestroyContext(cx); JS_DestroyRuntime(rt); JS_ShutDown(); system("pause"); exit(status); } char* ValToString(jsval val) { return JS_GetStringBytes(JS_ValueToString(cx, val)); } jsval NewJSString(const string& str) { char* buf = (char*) JS_malloc(cx, str.length()); memcpy(buf, str.c_str(), str.length()); return STRING_TO_JSVAL(JS_NewString(cx, buf, str.length())); } void ExecuteFile(const char* fileName) { FILE* f = fopen(fileName, "r"); if(!f) { cerr << "Cannot open " << fileName << endl; Shutdown(1); } string code; char buf[1025]; while(fgets(buf, 1024, f)) { code += buf; } jsval rval; JSBool ok = JS_EvaluateScript(cx, global, code.c_str(), code.length(), fileName, 1, &rval); if(!ok) Shutdown(1); } // Program entry point int main(int argc, char* argv[]) { InitJS(); if(argc!=3 && argc!=4) { cerr << "Usage: rmgen