try; add try implementation + details, update to latest main branch

This commit is contained in:
underscorediscovery
2021-04-03 21:58:31 -07:00
parent ecce1f6be9
commit 3c0fe12102
7 changed files with 463 additions and 10 deletions

View File

@ -607,8 +607,8 @@ var wasmMemory;
// In the wasm backend, we polyfill the WebAssembly object,
// so this creates a (non-native-wasm) table for us.
var wasmTable = new WebAssembly.Table({
'initial': 191,
'maximum': 191 + 0,
'initial': 205,
'maximum': 205 + 0,
'element': 'anyfunc'
});
@ -1229,11 +1229,11 @@ function updateGlobalBufferAndViews(buf) {
}
var STATIC_BASE = 1024,
STACK_BASE = 5271824,
STACK_BASE = 5271616,
STACKTOP = STACK_BASE,
STACK_MAX = 28944,
DYNAMIC_BASE = 5271824,
DYNAMICTOP_PTR = 28784;
STACK_MAX = 28736,
DYNAMIC_BASE = 5271616,
DYNAMICTOP_PTR = 28576;
assert(STACK_BASE % 16 === 0, 'stack must start aligned');
assert(DYNAMIC_BASE % 16 === 0, 'heap must start aligned');
@ -1817,7 +1817,7 @@ var ASM_CONSTS = {
// STATICTOP = STATIC_BASE + 27920;
// STATICTOP = STATIC_BASE + 27712;
/* global initializers */ __ATINIT__.push({ func: function() { ___wasm_call_ctors() } });
@ -1983,7 +1983,7 @@ var ASM_CONSTS = {
}
function _emscripten_get_sbrk_ptr() {
return 28784;
return 28576;
}
function _emscripten_memcpy_big(dest, src, num) {
@ -2185,10 +2185,10 @@ var dynCall_vii = Module["dynCall_vii"] = function() {
};
/** @type {function(...*):?} */
var dynCall_iii = Module["dynCall_iii"] = function() {
var dynCall_viii = Module["dynCall_viii"] = function() {
assert(runtimeInitialized, 'you need to wait for the runtime to be ready (e.g. wait for main() to be called)');
assert(!runtimeExited, 'the runtime was exited (use NO_EXIT_RUNTIME to keep it alive after main() exits)');
return Module["asm"]["dynCall_iii"].apply(null, arguments)
return Module["asm"]["dynCall_viii"].apply(null, arguments)
};
/** @type {function(...*):?} */
@ -2198,6 +2198,13 @@ var dynCall_iiii = Module["dynCall_iiii"] = function() {
return Module["asm"]["dynCall_iiii"].apply(null, arguments)
};
/** @type {function(...*):?} */
var dynCall_iii = Module["dynCall_iii"] = function() {
assert(runtimeInitialized, 'you need to wait for the runtime to be ready (e.g. wait for main() to be called)');
assert(!runtimeExited, 'the runtime was exited (use NO_EXIT_RUNTIME to keep it alive after main() exits)');
return Module["asm"]["dynCall_iii"].apply(null, arguments)
};
/** @type {function(...*):?} */
var dynCall_vi = Module["dynCall_vi"] = function() {
assert(runtimeInitialized, 'you need to wait for the runtime to be ready (e.g. wait for main() to be called)');

Binary file not shown.

39
try/main.try.c Normal file
View File

@ -0,0 +1,39 @@
#include "../test/test.h"
#include <stdio.h>
#include <string.h>
static WrenVM* vm = NULL;
//This is a simple program that exposes wren to the browser
//for https://wren.io/try and runs scripts.
static WrenVM* initVM()
{
WrenConfiguration config;
wrenInitConfiguration(&config);
config.resolveModuleFn = resolveModule;
config.loadModuleFn = readModule;
config.writeFn = vm_write;
config.errorFn = reportError;
// Might be a more reasonable value,
// but since this is simple, keep it simple.
config.initialHeapSize = 1024 * 1024 * 100;
return wrenNewVM(&config);
}
//The endpoint we call from the browser
int wren_compile(const char* input) {
WrenVM* vm = initVM();
WrenInterpretResult result = wrenInterpret(vm, "compile", input);
wrenFreeVM(vm);
return (int)result;
}
//Main not used, but required. We call wren_compile directly.
int main(int argc, const char* argv[]) {
return 0;
}

View File

@ -0,0 +1,59 @@
# Copied from projects/make and modified for emscripten
# To run, make sure emsdk is in your path (our sourced via their scripts)
# run `emmake make`
ifndef config
config=release_32bit
endif
ifndef verbose
SILENT = @
endif
ifeq ($(config),debug_32bit)
wren_config = debug_32bit
wren_try_config = debug_32bit
else ifeq ($(config),release_32bit)
wren_config = release_32bit
wren_try_config = release_32bit
else
$(error "invalid configuration $(config)")
endif
PROJECTS := wren wren_try
.PHONY: all clean help $(PROJECTS)
all: $(PROJECTS)
wren:
ifneq (,$(wren_config))
@echo "==== Building wren ($(wren_config)) ===="
@${MAKE} --no-print-directory -C . -f wren.make config=$(wren_config)
endif
wren_try: wren
ifneq (,$(wren_try_config))
@echo "==== Building wren_try ($(wren_try_config)) ===="
@${MAKE} --no-print-directory -C . -f wren_try.make config=$(wren_try_config)
endif
clean:
@${MAKE} --no-print-directory -C . -f wren.make clean
@${MAKE} --no-print-directory -C . -f wren_try.make clean
help:
@echo "Usage: make [config=name] [target]"
@echo ""
@echo "CONFIGURATIONS:"
@echo " release_32bit"
@echo " debug_32bit"
@echo ""
@echo "TARGETS:"
@echo " all (default)"
@echo " clean"
@echo " wren"
@echo " wren_try"
@echo ""

View File

@ -0,0 +1,171 @@
# Copied from projects/make and modified for emscripten
DEFINES += -D WREN_OPT_RANDOM -D WREN_OPT_META
LDFLAGS += " -s WASM=1 -s FILESYSTEM=0"
ifndef config
config=release_32bit
endif
ifndef verbose
SILENT = @
endif
.PHONY: clean prebuild
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
# Configurations
# #############################################
RESCOMP = windres
INCLUDES += -I../../src/include -I../../src/vm -I../../src/optional
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS += -lm
LDDEPS +=
LINKCMD = $(AR) -rcs "$@" $(OBJECTS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
ifeq ($(config),release_32bit)
TARGETDIR = ../../lib
TARGET = $(TARGETDIR)/libwren.bc
OBJDIR = obj/32bit/Release/wren
DEFINES += -DNDEBUG
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m32 -O3 -std=c99
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m32 -O3
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib32 -m32 -s
else ifeq ($(config),debug_32bit)
TARGETDIR = ../../lib
TARGET = $(TARGETDIR)/libwren_d.bc
OBJDIR = obj/32bit/Debug/wren
DEFINES += -DDEBUG
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m32 -g -std=c99
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m32 -g
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib32 -m32
else
$(error "invalid configuration $(config)")
endif
# Per File Configurations
# #############################################
# File sets
# #############################################
OBJECTS :=
OBJECTS += $(OBJDIR)/wren_compiler.o
OBJECTS += $(OBJDIR)/wren_core.o
OBJECTS += $(OBJDIR)/wren_debug.o
OBJECTS += $(OBJDIR)/wren_opt_meta.o
OBJECTS += $(OBJDIR)/wren_opt_random.o
OBJECTS += $(OBJDIR)/wren_primitive.o
OBJECTS += $(OBJDIR)/wren_utils.o
OBJECTS += $(OBJDIR)/wren_value.o
OBJECTS += $(OBJDIR)/wren_vm.o
# Rules
# #############################################
all: $(TARGET)
@:
$(TARGET): $(OBJECTS) $(LDDEPS) | $(TARGETDIR)
$(PRELINKCMDS)
@echo Linking wren
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
@echo Cleaning wren
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild: | $(OBJDIR)
$(PREBUILDCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) | $(PCH_PLACEHOLDER)
$(GCH): $(PCH) | prebuild
@echo $(notdir $<)
$(SILENT) $(CC) -x c-header $(ALL_CFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<"
$(PCH_PLACEHOLDER): $(GCH) | $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) touch "$@"
else
$(SILENT) echo $null >> "$@"
endif
else
$(OBJECTS): | prebuild
endif
# File Rules
# #############################################
$(OBJDIR)/wren_opt_meta.o: ../../src/optional/wren_opt_meta.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_opt_random.o: ../../src/optional/wren_opt_random.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_compiler.o: ../../src/vm/wren_compiler.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_core.o: ../../src/vm/wren_core.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_debug.o: ../../src/vm/wren_debug.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_primitive.o: ../../src/vm/wren_primitive.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_utils.o: ../../src/vm/wren_utils.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_value.o: ../../src/vm/wren_value.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/wren_vm.o: ../../src/vm/wren_vm.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(PCH_PLACEHOLDER).d
endif

View File

@ -0,0 +1,147 @@
# Copied from projects/make and modified for emscripten
DEFINES += -D WREN_OPT_RANDOM -D WREN_OPT_META
LDFLAGS += -s WASM=1 -s FILESYSTEM=0 -s EXIT_RUNTIME=0 -s ENVIRONMENT='web'
LDFLAGS += -s EXPORTED_FUNCTIONS='["_main", "_wren_compile"]'
LDFLAGS += -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
ifndef config
config=release_32bit
endif
ifndef verbose
SILENT = @
endif
.PHONY: clean prebuild
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
# Configurations
# #############################################
RESCOMP = windres
INCLUDES += -I../../src/include
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LINKCMD = $(CC) -o "$@.html" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
ifeq ($(config),release_32bit)
TARGETDIR = ../../bin
TARGET = $(TARGETDIR)/wren_try
OBJDIR = obj/32bit/Release/wren_try
DEFINES += -DNDEBUG
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m32 -O2 -std=c99
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m32 -O2
LIBS += ../../lib/libwren.bc -lm
LDDEPS += ../../lib/libwren.bc
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib32 -m32 -s
else ifeq ($(config),debug_32bit)
TARGETDIR = ../../bin
TARGET = $(TARGETDIR)/wren_try_d
OBJDIR = obj/32bit/Debug/wren_try
DEFINES += -DDEBUG
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -m32 -g -std=c99
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -m32 -g
LIBS += ../../lib/libwren_d.bc -lm
LDDEPS += ../../lib/libwren_d.bc
ALL_LDFLAGS += $(LDFLAGS) -L/usr/lib32 -m32
else
$(error "invalid configuration $(config)")
endif
# Per File Configurations
# #############################################
# File sets
# #############################################
OBJECTS :=
OBJECTS += $(OBJDIR)/main.try.o
OBJECTS += $(OBJDIR)/test.o
# Rules
# #############################################
all: $(TARGET)
@:
$(TARGET): $(OBJECTS) $(LDDEPS) | $(TARGETDIR)
$(PRELINKCMDS)
@echo Linking wren_try
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
@echo Cleaning wren_try
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild: | $(OBJDIR)
$(PREBUILDCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) | $(PCH_PLACEHOLDER)
$(GCH): $(PCH) | prebuild
@echo $(notdir $<)
$(SILENT) $(CC) -x c-header $(ALL_CFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<"
$(PCH_PLACEHOLDER): $(GCH) | $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) touch "$@"
else
$(SILENT) echo $null >> "$@"
endif
else
$(OBJECTS): | prebuild
endif
# File Rules
# #############################################
$(OBJDIR)/main.try.o: ../main.try.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/test.o: ../../test/test.c
@echo $(notdir $<)
$(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(PCH_PLACEHOLDER).d
endif

30
try/readme.md Normal file
View File

@ -0,0 +1,30 @@
# try wren implementation
This is the code to build the https://wren.io/try wasm component.
### How to build
- Install emscripten sdk from https://emscripten.org/
- Make the emsdk available to your terminal/PATH
- for example:
- source ~/dev/emsdk/emsdk_env.sh
- Run the emmake command to build
- emmake make
That should be all. This builds a js + wasm file for the page.
### How does the page work?
The page is at `doc/site/try/template.html`.
It loads `wren_try.js` which loads `wren_try.wasm`.
The page uses emscripten API to call the `wren_compile` C function, found in `main.try.c`.
The page hooks up printf/logging to the console for display.
### Notes
- The binaries land in bin/wren_try.wasm and bin/wren_try.js when building
- The default html output from emsripten is not used, doc/site/try/template.html is
- The wren_try.js and wren_try.wasm files are copied to doc/site/static
- The make project is a modified version of projects/make
- The code relies on code in test/