cppreeps icon indicating copy to clipboard operation
cppreeps copied to clipboard

Error while trying to run code as is in master

Open TylerSeanRau opened this issue 4 years ago • 3 comments
trafficstars

TypeError: Cannot read property 'location' of undefined
    at eval:9:2665
    at module.exports:25:12
    at main:6:13
    at eval:96:4
    at Object.<anonymous>:2:206408
    at Object.r.run:2:206917

TylerSeanRau avatar Jun 06 '21 01:06 TylerSeanRau

Turns out this repository needs to be updated for changes that have occurred in the emscripten tool chain over the years.

This diff got things working for me:

diff --git a/create.sh b/create.sh
index 04c5666..09bba04 100755
--- a/create.sh
+++ b/create.sh
@@ -4,11 +4,16 @@ mkdir -p dist
 mkdir -p build
 
 em++                                \
-    --std=c++11                     \
+    --std=c++17                     \
     --bind                          \
     -s WASM=1                       \
     -s MODULARIZE=1                 \
     -s ALLOW_MEMORY_GROWTH=1        \
+    -s WASM_ASYNC_COMPILATION=0     \
+    -s BINARYEN_ASYNC_COMPILATION=0 \
+    -s EXPORTED_FUNCTIONS='["_loop"]' \
+    -s "EXPORTED_RUNTIME_METHODS=['ccall']" \
+    -s ENVIRONMENT=shell            \
     -Iinclude -Ilib                 \
     -O3 -Wall -pedantic             \
         src/loop.cpp                \
diff --git a/include/cppreeps.hpp b/include/cppreeps.hpp
index e45af61..105e639 100644
--- a/include/cppreeps.hpp
+++ b/include/cppreeps.hpp
@@ -75,9 +75,6 @@ namespace screeps {
         val Memory      = get_global("Memory");
         val RawMemory   = get_global("RawMemory");
         val PathFinder  = get_global("PathFinder");
-        
-         tick_t() { std::printf(LOG_HEAD "TICK{%d} CONSTRUCTED\n", Game["time"].as<int>()); }
-        ~tick_t() { std::printf(LOG_HEAD "TICK{%d} DESTROYED\n",   Game["time"].as<int>()); }
     };
     
     static std::unique_ptr<tick_t> tick;
diff --git a/src/loop.cpp b/src/loop.cpp
index 6d38d51..2f48ae6 100644
--- a/src/loop.cpp
+++ b/src/loop.cpp
@@ -8,34 +8,17 @@
 /// Exporting lzw_xxcode() to current module
 #include <lzw.hpp>
 
+extern "C"
+{
 void loop() {
     using namespace utils;
     using namespace screeps;
     
     INIT();
     
-    std::printf("Updated Game.time = %d\n",
-        tick->Game["time"].as<int>());
+    std::printf("life\n");
     
-    std::printf("RawMemory.length = %d\n",
-        tick->RawMemory.call<val>("get")["length"].as<int>());
-    
-    std::printf("[OK, MOVE, RESOURCE_ENERGY] = [%d, %s, %s]\n",
-        gCONST("OK").as<int>(),
-        gCONST("MOVE").as<std::string>().c_str(),
-        gCONST("RESOURCE_ENERGY").as<std::string>().c_str());
-    
-    EM_ASM({
-        console.log("ASM: Game time  = " + Game.time);
-        console.log("ASM: Creeps num = " + _.size(Game.creeps));
-    });
-    
-    auto creeps_map = js_object_to_map(tick->Game["creeps"]);
-    for(auto const& kv : creeps_map) {
-        auto const& name  = kv.first;
-        auto const& creep = kv.second;
-        creep.call<int>("say", name);
-    }
+}
 }
 
 EMSCRIPTEN_BINDINGS(loop) {

TylerSeanRau avatar Jun 06 '21 08:06 TylerSeanRau

Something like:

diff --git a/src/main.js b/src/main.js
index 97fe88a..e7451df 100644
--- a/src/main.js
+++ b/src/main.js
@@ -5,6 +5,8 @@ const wasm_loader = require('wasm_loader')
 /// Module by XXX.js and XXX.wasm files
 const mod = wasm_loader('loop_mod', 'loop');
 
+global.Game = Game;
+global.Game = RawMemory;
 global.byteLength = function(str) {
     str = str + "";
     let len = str.length;

is also necessary to get at the memory

TylerSeanRau avatar Jun 06 '21 09:06 TylerSeanRau

Hi @TylerSeanRau! Thank you for report and actualizing binding code, repo is pretty old indeed. Feel free to make a PR, guess it will be very helpful to have an updated and working code.

Mototroller avatar Jun 06 '21 11:06 Mototroller