From f75077bee803152f68b6325f64bde3348eda9c8c Mon Sep 17 00:00:00 2001
From: Huy Nguyen <huy-nguyen@users.noreply.github.com>
Date: Mon, 1 Jun 2020 17:33:15 -0400
Subject: [PATCH] Fix test bed (#110)

Adapt the solution from #87 (comment) and fix script loading error to make the test bed work again.

Fixes #97.
---
 demo/html5canvas/testbed.html |  2 ++
 helpers/embox2d-helpers.js    | 12 ++++++------
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/demo/html5canvas/testbed.html b/demo/html5canvas/testbed.html
index e27c6e5..da27762 100755
--- a/demo/html5canvas/testbed.html
+++ b/demo/html5canvas/testbed.html
@@ -21,6 +21,8 @@
         <script type="text/javascript">  
             var Box2D;
             if (!Box2D) Box2D = (typeof Box2D !== 'undefined' ? Box2D : null) || Module;
+            Box2D = Box2D();
+            Module = Box2D;
             window.onload = function() {
                 using(Box2D, "b2.+")
                 init();
diff --git a/helpers/embox2d-helpers.js b/helpers/embox2d-helpers.js
index aedbcc2..d2eed64 100755
--- a/helpers/embox2d-helpers.js
+++ b/helpers/embox2d-helpers.js
@@ -42,11 +42,11 @@ function scaledVec2(vec, scale) {
 // http://stackoverflow.com/questions/12792486/emscripten-bindings-how-to-create-an-accessible-c-c-array-from-javascript
 function createChainShape(vertices, closedLoop) {
     var shape = new Box2D.b2ChainShape();            
-    var buffer = Box2D.allocate(vertices.length * 8, 'float', Box2D.ALLOC_STACK);
+    var buffer = Box2D._malloc(vertices.length * 8);
     var offset = 0;
     for (var i=0;i<vertices.length;i++) {
-        Box2D.setValue(buffer+(offset), vertices[i].get_x(), 'float'); // x
-        Box2D.setValue(buffer+(offset+4), vertices[i].get_y(), 'float'); // y
+        Box2D.HEAPF32[buffer + offset >> 2] = vertices[i].get_x();
+        Box2D.HEAPF32[buffer + (offset + 4) >> 2] = vertices[i].get_y();
         offset += 8;
     }            
     var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);
@@ -59,11 +59,11 @@ function createChainShape(vertices, closedLoop) {
 
 function createPolygonShape(vertices) {
     var shape = new Box2D.b2PolygonShape();            
-    var buffer = Box2D.allocate(vertices.length * 8, 'float', Box2D.ALLOC_STACK);
+    var buffer = Box2D._malloc(vertices.length * 8);
     var offset = 0;
     for (var i=0;i<vertices.length;i++) {
-        Box2D.setValue(buffer+(offset), vertices[i].get_x(), 'float'); // x
-        Box2D.setValue(buffer+(offset+4), vertices[i].get_y(), 'float'); // y
+        Box2D.HEAPF32[buffer + offset >> 2] = vertices[i].get_x();
+        Box2D.HEAPF32[buffer + (offset + 4) >> 2] = vertices[i].get_y();
         offset += 8;
     }            
     var ptr_wrapped = Box2D.wrapPointer(buffer, Box2D.b2Vec2);