diff --git a/Makefile b/Makefile index 52c7dfb..ae61dd3 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ test-all: raco make -v --disable-inline tests/test-all.rkt racket tests/test-all.rkt +test-browser-evaluate: + raco make -v --disable-inline tests/test-browser-evaluate.rkt + racket tests/test-browser-evaluate.rkt test-compiler: raco make -v --disable-inline tests/test-compiler.rkt diff --git a/js-assembler/assemble-helpers.rkt b/js-assembler/assemble-helpers.rkt index 9c3b163..513e63c 100644 --- a/js-assembler/assemble-helpers.rkt +++ b/js-assembler/assemble-helpers.rkt @@ -80,7 +80,7 @@ [(EnvPrefixReference? target) (assemble-prefix-reference target)] [(PrimitivesReference? target) - (format "MACHINE.primitives[~s]" (symbol->string (PrimitivesReference-name target)))] + (format "RUNTIME.Primitives[~s]" (symbol->string (PrimitivesReference-name target)))] [(ControlFrameTemporary? target) (assemble-control-frame-temporary target)] [(ModulePrefixTarget? target) diff --git a/js-assembler/mini-runtime.js b/js-assembler/mini-runtime.js index c8203cf..a2c39ab 100644 --- a/js-assembler/mini-runtime.js +++ b/js-assembler/mini-runtime.js @@ -1278,7 +1278,44 @@ + ////////////////////////////////////////////////////////////////////// + (function(scope) { + scope.ready = function(f) { + if (runtimeIsReady) { + notifyWaiter(f); + } else { + readyWaiters.push(f); + } + }; + scope.setReadyTrue = function() { + var i; + runtimeIsReady = true; + for (i = 0; i < readyWaiters.length; i++) { + notifyWaiter(readyWaiters[i]); + } + readyWaiters = []; + }; + + var runtimeIsReady = false; + var readyWaiters = []; + var notifyWaiter = function(w) { + setTimeout(w, 0); + }; + })(this); + ////////////////////////////////////////////////////////////////////// + + + + // Exports + exports['Primitives'] = Primitives; + + exports['ready'] = ready; + // Private: the runtime library will set this flag to true when + // the library has finished loading. + exports['setReadyTrue'] = setReadyTrue; + + exports['Machine'] = Machine; exports['Frame'] = Frame; exports['CallFrame'] = CallFrame; diff --git a/js-assembler/package.rkt b/js-assembler/package.rkt index e289a65..f9bb3a5 100644 --- a/js-assembler/package.rkt +++ b/js-assembler/package.rkt @@ -4,14 +4,16 @@ "quote-cdata.rkt" "../make.rkt" "../make-structs.rkt" - "get-runtime.rkt" + (prefix-in runtime: "get-runtime.rkt") (prefix-in racket: racket/base)) (provide package package-anonymous - package-standalone-xhtml) + package-standalone-xhtml + get-code + get-runtime) ;; Packager: produce single .js files to be included to execute a ;; program. Follows module dependencies. @@ -36,7 +38,7 @@ ;; indicates whether we should continue following module paths. ;; ;; The generated output defines a function called 'invoke' with -;; four arguments (MACHINE, SUCCESS, FAIL, PARAMS). When called, it'll +;; four arguments (MACHINE, SUCCESS, FAIL, PARAMS). When called, it will ;; execute the code to either run standalone expressions or ;; load in modules. (define (package source-code @@ -62,13 +64,15 @@ (fprintf op "var invoke = (function(MACHINE, SUCCESS, FAIL, PARAMS) {") - (make (cons only-bootstrapped-code - (list (make-MainModuleSource source-code))) + (fprintf op " plt.runtime.ready(function() {") + (make (list (make-MainModuleSource source-code)) packaging-configuration) + (fprintf op " });"); (fprintf op "});\n")) + ;; package-standalone-xhtml: X output-port -> void (define (package-standalone-xhtml source-code op) (display *header* op) @@ -78,6 +82,35 @@ +;; get-runtime: -> string +(define (get-runtime) + (let* ([buffer (open-output-string)] + [packaging-configuration + (make-Configuration + ;; should-follow? + (lambda (p) #t) + ;; on + (lambda (ast stmts) + (assemble/write-invoke stmts buffer) + (fprintf buffer "(MACHINE, function() { ")) + + ;; after + (lambda (ast stmts) + (fprintf buffer " }, FAIL, PARAMS);")) + + ;; last + (lambda () + (fprintf buffer "SUCCESS();")))]) + + (display (runtime:get-runtime) buffer) + + (fprintf buffer "(function(MACHINE, SUCCESS, FAIL, PARAMS) {") + (make (list only-bootstrapped-code) packaging-configuration) + (fprintf buffer "})(new plt.runtime.Machine(), function(){ plt.runtime.setReadyTrue(); }, function(){}, {});\n") + (get-output-string buffer))) + + + (define *header* #<string (car x))) @@ -78,6 +88,11 @@ (define (print-the-runtime) (display (get-runtime))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(define (get-javascript-code filename) + (display (get-code (make-ModuleSource (build-path filename))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;