1
0
mirror of https://github.com/janet-lang/janet synced 2026-05-20 20:32:16 +00:00

Web assembly build with emscripten.

This commit is contained in:
Calvin Rose
2018-10-16 23:08:26 -04:00
parent dde6218bff
commit c1923c5ada
12 changed files with 219 additions and 64 deletions
+27 -29
View File
@@ -792,8 +792,9 @@
(defn pp
"Pretty print a value. Displays values inside collections, and is safe
to call on any table. Does not print table prototype information."
[x]
@[x file]
(default file stdout)
(def buf @"")
(def indent @"\n")
(def seen @{})
@@ -853,12 +854,11 @@
(def complex? (> (length y) 4))
((if complex? pp-dict-nested pp-dict-simple) y))
(def printers {
:array (fn [y] (do-ds y "@[" "]" true pp-seq))
:tuple (fn [y] (do-ds y "(" ")" false pp-seq))
:table (fn [y] (do-ds y "@{" "}" true pp-dict))
:struct (fn [y] (do-ds y "{" "}" false pp-dict))
})
(def printers
{:array (fn [y] (do-ds y "@[" "]" true pp-seq))
:tuple (fn [y] (do-ds y "(" ")" false pp-seq))
:table (fn [y] (do-ds y "@{" "}" true pp-dict))
:struct (fn [y] (do-ds y "{" "}" false pp-dict))})
(:= recur (fn [y]
(def p (get printers (type y)))
@@ -869,7 +869,7 @@
(recur x)
(buffer.push-string buf "\n")
(file.write stdout buf)
(file.write file buf)
nil)
###
@@ -1041,8 +1041,8 @@
(def (line col) (parser.where p))
(onerr where "parse" (string (parser.error p) " on line " line ", column " col)))
(case (fiber.status chars)
:new (parser.byte p (resume chars))
:pending (parser.byte p (resume chars))
:new (parser.byte p (resume chars nil))
:pending (parser.byte p (resume chars nil))
(:= going false))))
(when (not= :root (parser.status p))
(onerr where "parse" "unexpected end of source"))))
@@ -1067,7 +1067,7 @@
err)
errf))))
:a))
(def res (resume f))
(def res (resume f nil))
(when good
(def sig (fiber.status f))
(if going
@@ -1078,18 +1078,18 @@
# Run loop
(def oldenv *env*)
(:= *env* env)
(while going (eval1 (resume vals)))
(while going (eval1 (resume vals nil)))
(:= *env* oldenv)
env)
(defn default-error-handler
@[source t x f]
(file.write stdout (string t " error in " source ": "))
(file.write stderr (string t " error in " source ": "))
(if (bytes? x)
(do (file.write stdout x)
(file.write stdout "\n"))
(pp x))
(do (file.write stderr x)
(file.write stderr "\n"))
(pp x stderr))
(when f
(def st (fiber.stack f))
(loop
@@ -1101,25 +1101,25 @@
:source source
:line source-line
:column source-col} :in st]
(file.write stdout " in")
(when c (file.write stdout " cfunction"))
(file.write stderr " in")
(when c (file.write stderr " cfunction"))
(if name
(file.write stdout " " name)
(when func (file.write stdout " " (string func))))
(file.write stderr " " name)
(when func (file.write stderr " " (string func))))
(if source
(do
(file.write stdout " [" source "]")
(file.write stderr " [" source "]")
(if source-line
(file.write
stdout
stderr
" on line "
(string source-line)
", column "
(string source-col)))))
(if (and (not source-line) pc)
(file.write stdout " (pc=" (string pc) ")"))
(when tail (file.write stdout " (tailcall)"))
(file.write stdout "\n"))))
(file.write stderr " (pc=" (string pc) ")"))
(when tail (file.write stderr " (tailcall)"))
(file.write stderr "\n"))))
(defn eval
"Evaluates a string in the current environment. If more control over the
@@ -1237,10 +1237,8 @@
(defn import* [env path & args]
(def targs (apply table args))
(def {
:as as
:prefix prefix
} targs)
(def {:as as
:prefix prefix} targs)
(def newenv (require path targs))
(var k (next newenv nil))
(def {:meta meta} newenv)
+7 -17
View File
@@ -28,24 +28,19 @@
/* Generated header */
#include <generated/core.h>
/* Only include dynamic modules if enabled */
#ifdef JANET_DYNAMIC_MODULES
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
* with native code. */
#ifdef JANET_WINDOWS
#if defined(JANET_NO_DYNAMIC_MODULES)
typedef int Clib;
#define load_clib(name) ((void) name, 0)
#define symbol_clib(lib, sym) ((void) lib, (void) sym, 0)
#define error_clib() "dynamic libraries not supported"
#elif defined(JANET_WINDOWS)
#include <windows.h>
typedef HINSTANCE Clib;
#define load_clib(name) LoadLibrary((name))
#define symbol_clib(lib, sym) GetProcAddress((lib), (sym))
#define error_clib() "could not load dynamic library"
#elif defined(JANET_WEB)
#include <emscripten.h>
/* TODO - figure out how loading modules will work in JS */
typedef int Clib;
#define load_clib(name) 0
#define symbol_clib(lib, sym) 0
#define error_clib() "dynamic libraries not supported"
#else
#include <dlfcn.h>
typedef void *Clib;
@@ -82,9 +77,6 @@ static int janet_core_native(JanetArgs args) {
JANET_RETURN_CFUNCTION(args, init);
}
#endif
/* end JANET_DYNAMIC_MODULES */
static int janet_core_print(JanetArgs args) {
int32_t i;
for (i = 0; i < args.n; ++i) {
@@ -295,9 +287,7 @@ static int janet_core_hash(JanetArgs args) {
}
static const JanetReg cfuns[] = {
#ifdef JANET_DYNAMIC_MODULES
{"native", janet_core_native},
#endif
{"print", janet_core_print},
{"describe", janet_core_describe},
{"string", janet_core_string},
@@ -605,7 +595,7 @@ JanetTable *janet_core_env(void) {
janet_def(env, "_env", ret);
/* Run bootstrap source */
janet_dobytes(env, janet_gen_core, sizeof(janet_gen_core), "core.janet");
janet_dobytes(env, janet_gen_core, sizeof(janet_gen_core), "core.janet", NULL);
return env;
}
+3
View File
@@ -158,6 +158,9 @@ static int janet_io_popen(JanetArgs args) {
flags = (fmode[0] == 'r') ? IO_PIPED | IO_READ : IO_PIPED | IO_WRITE;
#ifdef JANET_WINDOWS
#define popen _popen
#endif
#ifdef __EMSCRIPTEN__
#define popen(A, B) (errno = 0, NULL)
#endif
f = popen((const char *)fname, (const char *)fmode);
if (!f) {
+2
View File
@@ -45,6 +45,8 @@ static int os_which(JanetArgs args) {
JANET_RETURN_CSYMBOL(args, ":windows");
#elif __APPLE__
JANET_RETURN_CSYMBOL(args, ":macos");
#elif defined(__EMSCRIPTEN__)
JANET_RETURN_CSYMBOL(args, ":web");
#else
JANET_RETURN_CSYMBOL(args, ":posix");
#endif
+5 -4
View File
@@ -66,12 +66,13 @@ void janet_stacktrace(JanetFiber *fiber, const char *errtype, Janet err) {
}
/* Run a string */
int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath) {
int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out) {
JanetParser parser;
int errflags = 0;
int32_t index = 0;
int dudeol = 0;
int done = 0;
Janet ret = janet_wrap_nil();
const uint8_t *where = sourcePath ? janet_cstring(sourcePath) : NULL;
if (where) janet_gcroot(janet_wrap_string(where));
janet_parser_init(&parser);
@@ -85,7 +86,6 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char
if (cres.status == JANET_COMPILE_OK) {
JanetFunction *f = janet_thunk(cres.funcdef);
JanetFiber *fiber = janet_fiber(f, 64);
Janet ret = janet_wrap_nil();
JanetSignal status = janet_run(fiber, &ret);
if (status != JANET_SIGNAL_OK) {
janet_stacktrace(fiber, "runtime", ret);
@@ -126,12 +126,13 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char
}
janet_parser_deinit(&parser);
if (where) janet_gcunroot(janet_wrap_string(where));
if (out) *out = ret;
return errflags;
}
int janet_dostring(JanetTable *env, const char *str, const char *sourcePath) {
int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out) {
int32_t len = 0;
while (str[len]) ++len;
return janet_dobytes(env, (const uint8_t *)str, len, sourcePath);
return janet_dobytes(env, (const uint8_t *)str, len, sourcePath, out);
}
+1 -1
View File
@@ -57,7 +57,7 @@ void janet_symcache_deinit() {
}
/* Mark an entry in the table as deleted. */
#define JANET_SYMCACHE_DELETED ((const uint8_t *)0 + 1)
static const uint8_t JANET_SYMCACHE_DELETED[1] = {0};
/* Find an item in the cache and return its location.
* If the item is not found, return the location