Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
#include "dlfcn.h"12#define INCL_BASE3#include <os2.h>4#include <float.h>5#include <stdlib.h>6#include <string.h>7#include <stdio.h>89static ULONG retcode;10static char fail[300];1112static ULONG dllHandle;13static char dllname[80];14static int handle_found;15static int handle_loaded;1617#ifdef DLOPEN_INITTERM18unsigned long _DLL_InitTerm(unsigned long modHandle, unsigned long flag)19{20switch (flag) {21case 0: /* INIT */22/* Save handle */23dllHandle = modHandle;24handle_found = 1;25return TRUE;2627case 1: /* TERM */28handle_found = 0;29dllHandle = (unsigned long)NULLHANDLE;30return TRUE;31}32return FALSE;33}34#endif3536HMODULE37find_myself(void)38{39static APIRET APIENTRY (*pDosQueryModFromEIP)(HMODULE * hmod, ULONG * obj, ULONG BufLen, PCHAR Buf, ULONG * Offset, ULONG Address);40HMODULE doscalls_h, mod;41static int failed = 0;42ULONG obj, offset, rc;43char buf[260];4445if (failed) return 0;46failed = 1;47doscalls_h = (HMODULE)dlopen("DOSCALLS",0);48if (!doscalls_h) return 0;49/* {&doscalls_handle, NULL, 360}, */ /* DosQueryModFromEIP */50rc = DosQueryProcAddr(doscalls_h, 360, 0, (PFN*)&pDosQueryModFromEIP);51if (rc) return 0;52rc = pDosQueryModFromEIP(&mod, &obj, sizeof(buf), buf, &offset, (ULONG)dlopen);53if (rc) return 0;54failed = 0;55handle_found = 1;56dllHandle = mod;57return mod;58}5960void *61dlopen(char *path, int mode)62{63HMODULE handle;64char tmp[260], *beg, *dot;65ULONG rc;66unsigned fpflag = _control87(0,0);6768fail[0] = 0;69if (!path) { /* Our own handle. */70if (handle_found || find_myself()) {71if (handle_loaded) return (void*)dllHandle;72rc = DosQueryModuleName(dllHandle, sizeof(dllname), dllname);73if (rc) {74strcpy(fail, "can't find my DLL name by the handle");75retcode = rc; return 0;76}77rc = DosLoadModule((PSZ)fail, sizeof fail, (PSZ)dllname, &handle);78if (rc) {79strcpy(fail, "can't load my own DLL");80retcode = rc; return 0;81}82handle_loaded = 1;83goto ret;84}85retcode = ERROR_MOD_NOT_FOUND;86strcpy(fail, "can't load from myself: compiled without -DDLOPEN_INITTERM");87return 0;88}89if ((rc = DosLoadModule((PSZ)fail, sizeof fail, (PSZ)path, &handle)) == 0)90goto ret;9192retcode = rc;93/* Not found. Check for non-FAT name and try truncated name. */94/* Don't know if this helps though... */95for (beg = dot = path + strlen(path);96beg > path && !strchr(":/\\", *(beg-1));97beg--)98if (*beg == '.') dot = beg;99if (dot - beg > 8) {100int n = beg+8-path;101memmove(tmp, path, n);102memmove(tmp+n, dot, strlen(dot)+1);103rc = DosLoadModule((PSZ)fail, sizeof fail, (PSZ)tmp, &handle);104if (rc == 0) goto ret;105retcode = rc;106}107handle = 0;108109ret:110_control87(fpflag, MCW_EM); /* Some modules reset FP flags on load */111return (void *)handle;112}113114#define ERROR_WRONG_PROCTYPE 0xffffffff115116void *117dlsym(void *handle, char *symbol)118{119ULONG rc, type;120PFN addr;121122fail[0] = 0;123rc = DosQueryProcAddr((HMODULE)handle, 0, (PSZ)symbol, &addr);124if (rc == 0) {125rc = DosQueryProcType((HMODULE)handle, 0, (PSZ)symbol, &type);126if (rc == 0 && type == PT_32BIT) return (void *)addr;127rc = ERROR_WRONG_PROCTYPE;128}129retcode = rc; return NULL;130}131132char *133dlerror(void)134{135static char buf[700];136ULONG len;137138if (retcode == 0) return NULL;139if (retcode == ERROR_WRONG_PROCTYPE) {140strcpy(buf, "Wrong procedure type");141len = strlen(buf);142}143if ((retcode != ERROR_WRONG_PROCTYPE)144&& DosGetMessage(NULL, 0, buf, sizeof buf - 1, retcode,145(PSZ)"OSO001.MSG", &len)) {146if (fail[0])147sprintf(buf, "OS/2 system error code %d, problematic module: '%s'",148(int)retcode, fail);149else150sprintf(buf, "OS/2 system error code %d", (int)retcode);151} else {152buf[len] = '\0';153if (len && buf[len - 1] == '\n') buf[--len] = 0;154if (len && buf[len - 1] == '\r') buf[--len] = 0;155if (len && buf[len - 1] == '.') buf[--len] = 0;156if (fail[0] && len < 300)157sprintf(buf + len, ", problematic module: '%s'", fail);158}159retcode = 0; return buf;160}161162int163dlclose(void *handle)164{165ULONG rc;166167if ((rc = DosFreeModule((HMODULE)handle)) == 0) return 0;168retcode = rc; return 2;169}170171void*172get_stack(double fraction, long min)173{174int rc;175TIB *tib;176PIB *pib;177char *s, *e;178unsigned long d;179180if (!(_emx_env & 0x200)) return 0; /* not OS/2. */181rc = DosGetInfoBlocks(&tib, &pib);182if (rc) return 0; /* ignore error */183s = (char*)tib->tib_pstack;184e = (char*)tib->tib_pstacklimit;185d = fraction * (e-s);186if (min >= 3*(e-s)/4) min = 3*(e-s)/4;187if (d < min) d = min;188return (void*)(s + d);189}190191192