Microsoft MakeCode

C++ extensions issues

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())
})

What is wrong ? Thanks for your support !

pxt-adafruit/libs/x1/x1.cpp:

#include “pxt.h”
namespace x1namespace {
/**
* Reads something
*/
//% blockId=x1readSomething block=“readSomething”
int readSomething() {
return 56;
}
}

pxt-adafruit/libs/x1/x1.ts:

/**

  • access to x1
    */
    //% color=190 weight=100 block=“x1”
    namespace x1namespace {
    function readSomething(): int32 {
    return 57;
    }
    }

pxt-adafruit/libs/x1/pxt.json:

{
“name”: “x1”,
“description”: “experimental x1”,
“files”: [
“x1.cpp”,
“x1.ts”,
“shims.d.ts”
],
“public”: true,
“dependencies”: {
“core”: “file:…/core”
}
}

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?

@richard @jwunderl @shakao