+ for (;;) {
+ dirent setup;
+ dirent *result;
+ _syscall(readdir_r(setups, &setup, &result));
+
+ if (result == NULL)
+ break;
+ _assert(result == &setup);
+
+ const char *name(setup.d_name);
+ size_t length(strlen(name));
+ if (length < 4)
+ continue;
+
+ if (name[0] == '.')
+ continue;
+ if (memcmp(name + length - 3, ".cy", 3) != 0)
+ continue;
+
+ std::string script(folder + "/" + name);
+ CYUTF8String utf8;
+ utf8.data = reinterpret_cast<char *>(CYMapFile(script.c_str(), &utf8.size));
+
+ CYPool pool;
+ CYUTF16String utf16(CYPoolUTF16String(pool, utf8));
+ munmap(const_cast<char *>(utf8.data), utf8.size);
+
+ // XXX: this should not be used
+ CydgetMemoryParse(&utf16.data, &utf16.size);
+
+ CYExecute(context, pool, CYPoolUTF8String(pool, utf16));
+ free(const_cast<uint16_t *>(utf16.data));
+ }
+
+ _syscall(closedir(setups));
+}
+
+static JSValueRef require(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ _assert(count == 1);
+ CYPool pool;
+
+ Dl_info addr;
+ _assert(dladdr(reinterpret_cast<void *>(&require), &addr) != 0);
+ char *lib(pool.strdup(addr.dli_fname));
+
+ char *slash(strrchr(lib, '/'));
+ _assert(slash != NULL);
+ *slash = '\0';
+
+ CYJSString property("exports");
+ JSObjectRef module;
+
+ const char *path(pool.strcat(lib, "/cycript/", CYPoolCString(pool, context, arguments[0]), ".cy", NULL));
+ CYJSString key(path);
+ JSObjectRef modules(CYGetCachedObject(context, CYJSString("modules")));
+ JSValueRef cache(CYGetProperty(context, modules, key));
+
+ if (!JSValueIsUndefined(context, cache))
+ module = CYCastJSObject(context, cache);
+ else {
+ CYUTF8String code;
+ code.data = reinterpret_cast<char *>(CYMapFile(path, &code.size));
+
+ std::stringstream wrap;
+ wrap << "(function (exports, require, module) { " << code << "\n});";
+ code = CYPoolCode(pool, wrap.str().c_str());
+
+ JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0));
+ JSObjectRef function(CYCastJSObject(context, value));
+
+ module = JSObjectMake(context, NULL, NULL);
+ JSObjectRef exports(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, module, property, exports);
+
+ JSValueRef arguments[3] = { exports, JSObjectMakeFunctionWithCallback(context, CYJSString("require"), &require), module };
+ CYCallAsFunction(context, function, NULL, 3, arguments);
+ CYSetProperty(context, modules, key, module);
+ }
+
+ return CYGetProperty(context, module, property);
+} CYCatch(NULL) }
+
+extern "C" void CYSetupContext(JSGlobalContextRef context) {