Closed JeanChristopheMorinPerso closed 1 year ago
It looks inheritance for static class functions was never implemented. The embind code pre-dates JS classes so it was probably just hard to implement this at the time using prototype inheritance.
Thanks for the answer. After looking into it, it seems like it might be simple to implement. Or at least I'm hoping that it's simple enough. I'll see how far I can get.
Basically, from what I see, ES5 classes don't inherit class functions by default. You have to do it manually. With ES6 classes, class functions are automatically inherited. https://www.bennadel.com/blog/3300-static-methods-are-inherited-when-using-es6-extends-syntax-in-javascript-and-node-js.htm.
Example in pure JS:
// Create class A without syntax sugar
var A = function () { }
A.class_function = function () {
console.log(`${this.name}.class_function`)
}
// Create class B without syntax sugar
var B = function () { }
var instanceProperty = Object.create(A.prototype, {
constructor: { value: B }
})
B.prototype = instanceProperty
// Test if class_function exists on class B. It should print false.
console.log(B.hasOwnProperty('class_function'))
for (const key in A) {
if (A.hasOwnProperty(key) && typeof (A[key] == 'function')) {
B[key] = A[key]
}
}
// Test if class_function exists on class B. It should print true.
console.log(B.hasOwnProperty('class_function'))
A.class_function()
B.class_function()
class C {
static class_function() {
console.log(`${this.name}.class_function`)
}
}
class D extends C {
}
C.class_function()
D.class_function()
outputs
false
true
A.class_function
B.class_function
C.class_function
D.class_function
Please include the following in your bug report:
Version of emscripten/emsdk:
Problem:
When binding classes using Embind, functions declared as class functions are not available in subclasses. For example, I have this code:
and I compile it with
emcc -lembind -o lib.js bindings.cpp -s MODULARIZE=1
, and then run it in JS:It raises:
We can see that that
A.from_json_string()
works butB.from_json_string()
andC.from_json_string()
don't work.I was kind of expecting class function to be available in sub-classes... Is this a bug or just how it should work?
Thanks a lot!