khchen / nimpk

PocketLang binding for Nim
MIT License
20 stars 1 forks source link

NimPK on Embedded Systems #1

Open davidvfx07 opened 3 months ago

davidvfx07 commented 3 months ago

I've been interested in creating a language that can be interpreted on embedded systems (esp32) with platformio for use in a larger project, which would provide an api to the user which calls precompiled procs that talk to the hardware. I came across nimpk which compiles great and works well on PC, but there are some weird memory issues on esp32, causing Guru Meditation errors when registering libs (backtrace attached bellow).

Is there some solvable problem that would be causing a memory leak or something that would cause the esp not to boot?

Ultimately this may be impossible given the minuscule power of the esp32 to interpret nimpk on its own, and there may be other unforeseen reasons this won't work, but it would be a great tool for embedded programming and super helpful for the community.

PS. Since importing nimpk/src requires an os, and is necessary to include pocketlang libs into the compile, it has to be reimplemented in platformio for the platformio compiler. By default, pocketlang.h is not included in the compile without nimpk/src, so adding {importc, header: "pocketlang.h"} in nimpk/includes.nim includes it. It will then throw errors like error: use of enum 'PkVarType' without previous declaration, which can be circumvented by reordering pocketlang.h. Also be sure to use an environment with both arduino and esp-idf together. This is because it seems as though pocketlang requires lower level functionality from the esp-idf that's hidden by using just arduino, which is then resurfaced with esp-idf.


Full backtrace:

Backtrace: 0x4200ac8c:0x3fcf3910 0x4200acc7:0x3fcf3930 0x4200acf9:0x3fcf3950 0x420173ed:0x3fcf3980 0x420086d2:0x3fcf39b0 0x4201089c:0x3fcf39d0 0x4200f2dd:0x3fcf39f0 0x4200856a:0x3fcf3a10 0x420050f5:0x3fcf3a50 0x42008109:0x3fcf3a70 0x420082c7:0x3fcf3ac0 0x420082d2:0x3fcf3ae0 0x420082de:0x3fcf3b00 0x4203d510:0x3fcf3b20 0x4037d7d1:0x3fcf3b40

  #0  0x4200ac8c:0x3fcf3910 in moduleGetStringAt at lib/pocketlang/core/value.c:1335
  #1  0x4200acc7:0x3fcf3930 in moduleGetGlobalIndex at lib/pocketlang/core/value.c:1366
  #2  0x4200acf9:0x3fcf3950 in moduleSetGlobal at lib/pocketlang/core/value.c:1347
  #3  0x420173ed:0x3fcf3980 in moduleAddFunctionInternal at lib/pocketlang/core/core.c:967
  #4  0x420086d2:0x3fcf39b0 in pkModuleAddFunction at lib/pocketlang/core/public.c:277
  #5  0x4201089c:0x3fcf39d0 in registerModuleAlgorithm at lib/pocketlang/libs/std_algorithm.c:330
  #6  0x4200f2dd:0x3fcf39f0 in registerLibs at lib/pocketlang/libs/libs.c:34
  #7  0x4200856a:0x3fcf3a10 in pkNewVM at lib/pocketlang/core/public.c:153
  #8  0x420050f5:0x3fcf3a50 in newVm__OOZnimlibZnimpk_u257(PkConfiguration*) at src/nimcache/@m..@snimlib@snimpk.nim.cpp:316
  #9  0x42008109:0x3fcf3a70 in NimMainModule() at src/nimcache/@mmain.nim.cpp:528
  #10 0x420082c7:0x3fcf3ac0 in NimMainInner() at src/nimcache/@mmain.nim.cpp:511
  #11 0x420082d2:0x3fcf3ae0 in NimMain at src/nimcache/@mmain.nim.cpp:522
  #12 0x420082de:0x3fcf3b00 in app_main at src/nimcache/@mmain.nim.cpp:481
  #13 0x4203d510:0x3fcf3b20 in main_task at /Users/user/.platformio/packages/framework-espidf/components/freertos/port/port_common.c:141 (discriminator 2)
  #14 0x4037d7d1:0x3fcf3b40 in vPortTaskWrapper at /Users/user/.platformio/packages/framework-espidf/components/freertos/port/xtensa/port.c:142
davidvfx07 commented 3 months ago

I made an issue on the official pocketlang repo in case somebody there can help out, ThakeeNathees/pocketlang#300.

davidvfx07 commented 3 months ago

UPDATE. Moved to RP2040 for convenience. I think I'm going to stay here and manually implement the libraries I want from within Nim. The std_lib isn't very important to the project anyway. I am still doing a lot of testing around memory...