move random number gen

This commit is contained in:
2025-11-29 14:22:19 -06:00
parent a9cff079d9
commit efe93b7206
5 changed files with 363 additions and 425 deletions

View File

@@ -157,33 +157,6 @@ static uint32_t xorshift32(){
return rng_state = x;
}
JSC_CCALL(os_guid,
uint8_t data[16];
for(int i = 0; i < 4; i++){
uint32_t v = xorshift32();
memcpy(&data[i*4], &v, 4);
}
static const char hex[] = "0123456789abcdef";
char buf[32];
for(int i = 0; i < 16; i++){
uint8_t b = data[i];
buf[i*2 ] = hex[b >> 4];
buf[i*2 + 1] = hex[b & 0x0f];
}
return JS_NewStringLen(js, buf, 32);
)
JSC_SCALL(console_print, printf("%s", str); )
static const JSCFunctionListEntry js_console_funcs[] = {
MIST_FUNC_DEF(console,print,1),
};
JSC_SCALL(os_system, ret = number2js(js,system(str)); )
JSC_CCALL(os_rand,
MTRand *mrand = &((cell_rt*)JS_GetContextOpaque(js))->mrand;
return number2js(js, genRand(mrand));
@@ -199,124 +172,6 @@ JSC_CCALL(os_srand,
m_seedRand(mrand, js2number(js,argv[0]));
)
static const JSCFunctionListEntry js_util_funcs[] = {
MIST_FUNC_DEF(os, guid, 0),
};
static void *get_main_module_handle() {
#if defined(_WIN32)
return GetModuleHandle(NULL);
#else
return dlopen(NULL, RTLD_LAZY);
#endif
}
static void *get_symbol(void *handle, const char *name) {
#if defined(_WIN32)
return (void*)GetProcAddress((HMODULE)handle, name);
#else
return dlsym(handle, name);
#endif
}
JSC_CCALL(os_use_dyn,
const char *path = JS_ToCString(js, argv[0]);
if (!path) return JS_ThrowTypeError(js, "path must be a string");
// 1. Load the library
#if defined(_WIN32)
HMODULE handle = LoadLibraryA(path);
#else
void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
#endif
if (!handle) {
JS_FreeCString(js, path);
#if defined(_WIN32)
return JS_ThrowReferenceError(js, "Could not load library");
#else
return JS_ThrowReferenceError(js, "Could not load library: %s", dlerror());
#endif
}
// 2. Construct symbol name: js_<basename>_use
// Extract basename from path
const char *base = strrchr(path, '/');
#if defined(_WIN32)
const char *base2 = strrchr(path, '\\');
if (base2 > base) base = base2;
#endif
if (base) base++; // Skip the slash
else base = path; // No slash, use whole path
char name_buf[256];
const char *dot = strrchr(base, '.');
size_t len = dot ? (size_t)(dot - base) : strlen(base);
if (len > 100) len = 100; // Safety cap
char clean_base[128];
memcpy(clean_base, base, len);
clean_base[len] = '\0';
snprintf(name_buf, sizeof(name_buf), "js_%s_use", clean_base);
// 3. Get the symbol
typedef JSValue (*init_fn)(JSContext*);
init_fn fn = (init_fn)get_symbol(handle, name_buf);
JS_FreeCString(js, path);
if (!fn) {
// Try without stripping extension? No, standard is usually without.
// Maybe try "js_main_use"? No.
// Let's stick to the plan.
#if defined(_WIN32)
FreeLibrary(handle);
#else
dlclose(handle);
#endif
return JS_ThrowReferenceError(js, "Could not find entry point %s in library", name_buf);
}
// 4. Call the function
return fn(js);
)
JSC_SCALL(os_load_internal,
void *handle = get_main_module_handle();
if (!handle) {
return JS_ThrowReferenceError(js, "Could not get main module handle");
}
JSValue (*js_use)(JSContext*) = get_symbol(handle, str);
if (!js_use) {
// Try without "js_" prefix or other variations if needed, but standard is js_<name>_use
return JS_NULL;
}
ret = js_use(js);
)
JSValue js_util_use(JSContext *js) {
JSValue mod = JS_NewObject(js);
JS_SetPropertyFunctionList(js,mod,js_util_funcs,countof(js_util_funcs));
return mod;
}
JSValue js_console_use(JSContext *js) {
JSValue mod = JS_NewObject(js);
JS_SetPropertyFunctionList(js,mod,js_console_funcs,countof(js_console_funcs));
return mod;
}
JSC_CCALL(os_value_id,
JS_SetPropertyStr(js, argv[0], "id", number2js(js, (uintptr_t)JS_VALUE_GET_PTR(argv[0])));
return argv[0];
)
void ffi_load(JSContext *js)
{
cell_rt *rt = JS_GetContextOpaque(js);
@@ -336,13 +191,7 @@ void ffi_load(JSContext *js)
JSValue hidden_fn = JS_NewObject(js);
// Add functions that should only be accessible to engine.js
JS_SetPropertyStr(js, hidden_fn, "load_internal", JS_NewCFunction(js, js_os_load_internal, "load_internal", 1));
JS_SetPropertyStr(js, hidden_fn, "rand", JS_NewCFunction(js, js_os_rand, "rand", 0));
JS_SetPropertyStr(js, hidden_fn, "randi", JS_NewCFunction(js, js_os_randi, "randi", 0));
JS_SetPropertyStr(js, hidden_fn, "srand", JS_NewCFunction(js, js_os_srand, "srand", 1));
JS_SetPropertyStr(js, hidden_fn, "use_dyn", JS_NewCFunction(js, js_os_use_dyn, "use_dyn", 1));
#if defined(_WIN32)
JS_SetPropertyStr(js, hidden_fn, "dylib_ext", JS_NewString(js, ".dll"));
#elif defined(__APPLE__)