emscripten-core / emscripten-core/emscripten

WebIDL static on prototype vs. function/class definition is not correct

Open
#12,676 4 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

In JavaScript, and really in all languages as I understand it, what `static` really means is that it is a member of the class definition rather than the instance.

In Emscripten's parsing of WebIDL, it takes these static methods and places them on the prototype. I am really not sure what the thinking was here, I would be curious to know, but in JS, this is how we think of `static`:

```
// WebIDL
interface Person {
void Person();
static long TotalPeople;
static Person Create (DOMString name);
}
// C++ (ish, forgive me if not perfect)
struct Person
{
static uint32_t TotalPeople = 0;
std::string Name;
Person()
{
TotalPeople += 1;
}
static Person Create(std::string name)
{
Person p;
p.Name = name;
return p;
}
}
// JavaScript
function Person (){}
Person.prototype.Name = "";
Person.TotalPeople = 0;
Person.Create = function(name) {
var p = new Person();
p.Name = name;
return p;
}
```
`static` is really just a property directly on the class (function).

IMHO it feels quite dirty accessing the prototype in this way, but I do stand ready to be slapped into rethinking my understanding of this, if necessary. However, I think the WebIDL spec is on my side here: https://heycam.github.io/webidl/#idl-static-attributes-and-operations

```
[Exposed=Window]
interface Circle {
// ... omitted for brevity
static Point triangulate(Circle c1, Circle c2, Circle c3);
};

// ... omitted for brevity

typeof Circle.triangulate; // Evaluates to "function"
Circle.prototype.triangulate; // Evaluates to undefined
```
FWIW in my own local testing, I am not seeing static WebIDL definitions even show up on the prototype, which is what led me down this road. I haven't ruled out a user issue yet so this post isn't about that, but I am concerned about the emscripten interpretation of WebIDL static.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.