I am working on a C++ extension for a new hardware+editor, but need support. I get it to compile, but it fails with “Cannot read property <> of undefined” in the simulator/browser. I must be doing something wrong with namespaces?!
I used pxt-adafruit as a starting point, but I don’t think it is specific to that. It is not going to be a downloadable extension since it is tied to the hardware. So here are my steps:
setup files like this (content at the bottom, very short files)
pxt-adafruit/libs/x1/pxt.json
pxt-adafruit/libs/x1/x1.cpp
pxt-adafruit/libs/x1/x1.ts
pxt-adafruit/libs/x1/shims.d.ts (autogenerated)
add x1 to pxtarget.json, and libs/circuit-playground/pxt.json
It compiles, the shims.d.ts is successfully auto-generated, but I this simple test fails with "Cannot read property ‘readSomething’ of undefined.
forever(function () {
console.logValue(“x”, x1namespace.readSomething())
})
We finally figured this out. In case someone else stumbles. So we wanted to add a cpp lib to the project (using pxt-adafruit as a starting point). I think the main problem was that the simulator wouldn’t find a typescript counterpart. This is “solution 1”:
create the cpp file with all block and function annotations using //%
create the pxt.json file including the “shim.d.ts” and “enums.d.ts” files which will be autogenerated
add the lib to pxtarget.json and also as a dependency to the (in this case) libs/circuit-playground/pxt.json (otherwise you lib/extension doesn’t show in the editor without first adding the extension)
and (this is what we were missing)
add a /sim/< libname >.ts file in your extension folder with a namespace pxsim.< your namespace >, which exports the typescript counter part function to your cpp functions
add a “///<reference path=” to the simulator, in this case /sim/dalboard.ts
“solution 2” was inspired by peli in an earlier version of pxt-midi. peli only
created a cpp file and
create the corresponding ts file with all the //% annotations in the extension, and
manually added a //% shim= reference in the ts file to the cpp function, and
'export’ed the ts function for the simulator to find
This does not (need to) autogenerate any .d.ts files. Suprisingly (to us) the simulator finds the exported function without the addition of a /sim folder and without a namespace pxsim.< your namespace > .
@peli could you possibly shed some light on this for us?
I know this is an old thread, but I’m having the same sort of problem with a micro:bit extension and would appreciate any suggestions. I think I’ve followed the solution described above, but maybe I’m missing something:
Answering my question for anyone else who encounters this problem (I think it’s also answered elsewhere).
My extension is entirely C++ (no simulation component). Many TypeScript functions were just place holders with empty bodies and the C++ complement was the important part.
The error was caused by the methods with the empty bodies. I added a simple return to all the empty bodies, which seems to have resolved the problem.