+static const char *CYPoolLibraryPath(CYPool &pool) {
+ Dl_info addr;
+ _assert(dladdr(reinterpret_cast<void *>(&CYPoolLibraryPath), &addr) != 0);
+ char *lib(pool.strdup(addr.dli_fname));
+
+ char *slash(strrchr(lib, '/'));
+ _assert(slash != NULL);
+ *slash = '\0';
+
+ slash = strrchr(lib, '/');
+ if (slash != NULL && strcmp(slash, "/.libs") == 0)
+ *slash = '\0';
+
+ return lib;
+}
+
+static JSValueRef require_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ _assert(count == 1);
+ CYPool pool;
+
+ const char *name(CYPoolCString(pool, context, arguments[0]));
+ if (strchr(name, '/') == NULL && (
+#ifdef __APPLE__
+ dlopen(pool.strcat("/System/Library/Frameworks/", name, ".framework/", name, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL ||
+ dlopen(pool.strcat("/System/Library/PrivateFrameworks/", name, ".framework/", name, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL ||
+#endif
+ false))
+ return CYJSUndefined(context);
+
+ JSObjectRef resolve(CYCastJSObject(context, CYGetProperty(context, object, CYJSString("resolve"))));
+ CYJSString path(context, CYCallAsFunction(context, resolve, NULL, 1, arguments));
+
+ CYJSString property("exports");
+
+ JSObjectRef modules(CYGetCachedObject(context, CYJSString("modules")));
+ JSValueRef cache(CYGetProperty(context, modules, path));
+
+ JSValueRef result;
+ if (!JSValueIsUndefined(context, cache)) {
+ JSObjectRef module(CYCastJSObject(context, cache));
+ result = CYGetProperty(context, module, property);
+ } else {
+ CYUTF8String code(CYPoolFileUTF8String(pool, CYPoolCString(pool, context, path)));
+ _assert(code.data != NULL);
+
+ size_t length(strlen(name));
+ if (length >= 5 && strcmp(name + length - 5, ".json") == 0) {
+ JSObjectRef JSON(CYGetCachedObject(context, CYJSString("JSON")));
+ JSObjectRef parse(CYCastJSObject(context, CYGetProperty(context, JSON, CYJSString("parse"))));
+ JSValueRef arguments[1] = { CYCastJSValue(context, CYJSString(code)) };
+ result = CYCallAsFunction(context, parse, JSON, 1, arguments);
+ } else {
+ JSObjectRef module(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, modules, path, module);
+
+ JSObjectRef exports(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, module, property, exports);
+
+ std::stringstream wrap;
+ wrap << "(function (exports, require, module, __filename) { " << code << "\n});";
+ code = CYPoolCode(pool, *wrap.rdbuf());
+
+ JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0));
+ JSObjectRef function(CYCastJSObject(context, value));
+
+ JSValueRef arguments[4] = { exports, object, module, CYCastJSValue(context, path) };
+ CYCallAsFunction(context, function, NULL, 4, arguments);
+ result = CYGetProperty(context, module, property);
+ }
+ }
+
+ return result;
+} CYCatch(NULL) }
+
+static bool CYRunScript(JSGlobalContextRef context, const char *path) {
+ CYPool pool;
+ CYUTF8String code(CYPoolFileUTF8String(pool, pool.strcat(CYPoolLibraryPath(pool), path, NULL)));
+ if (code.data == NULL)
+ return false;
+
+ code = CYPoolCode(pool, code);
+ _jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0);
+ return true;
+}
+
+extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak, void *data) {
+}
+