Skip to content

Commit 4765f9c

Browse files
authored
Allow classes in JS library elements (#21028)
1 parent 7a14e5b commit 4765f9c

25 files changed

+100
-102
lines changed

src/embind/embind_gen.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@ var LibraryEmbind = {
88

99
$moduleDefinitions: [],
1010

11-
$PrimitiveType: class PrimitiveType {
11+
$PrimitiveType: class {
1212
constructor(typeId, name, destructorType) {
1313
this.typeId = typeId;
1414
this.name = name;
1515
this.destructorType = destructorType;
1616
}
1717
},
18-
$IntegerType: class IntegerType {
18+
$IntegerType: class {
1919
constructor(typeId) {
2020
this.typeId = typeId;
2121
this.destructorType = 'none';
2222
}
2323
},
24-
$Argument: class Argument {
24+
$Argument: class {
2525
constructor(name, type) {
2626
this.name = name;
2727
this.type = type;
2828
}
2929
},
30-
$UserType: class UserType {
30+
$UserType: class {
3131
constructor(typeId, name) {
3232
this.typeId = typeId;
3333
this.name = name;
3434
this.destructorType = 'none'; // Same as emval.
3535
}
3636
},
3737
$FunctionDefinition__deps: ['$createJsInvoker'],
38-
$FunctionDefinition: class FunctionDefinition {
38+
$FunctionDefinition: class {
3939
constructor(name, returnType, argumentTypes, functionIndex, thisType = null, isAsync = false) {
4040
this.name = name;
4141
this.returnType = returnType;
@@ -106,7 +106,7 @@ var LibraryEmbind = {
106106
);
107107
}
108108
},
109-
$PointerDefinition: class PointerDefinition {
109+
$PointerDefinition: class {
110110
constructor(classType, isConst, isSmartPointer) {
111111
this.classType = classType;
112112
this.isConst = isConst;
@@ -117,7 +117,7 @@ var LibraryEmbind = {
117117
}
118118
}
119119
},
120-
$ClassDefinition: class ClassDefinition {
120+
$ClassDefinition: class {
121121
constructor(typeId, name, base = null) {
122122
this.typeId = typeId;
123123
this.name = name;
@@ -200,7 +200,7 @@ var LibraryEmbind = {
200200
}
201201

202202
},
203-
$ClassProperty: class ClassProperty {
203+
$ClassProperty: class {
204204
constructor(type, name, readonly) {
205205
this.type = type;
206206
this.name = name;
@@ -211,7 +211,7 @@ var LibraryEmbind = {
211211
out.push(`${this.readonly ? 'readonly ' : ''}${this.name}: ${nameMap(this.type)}`);
212212
}
213213
},
214-
$ConstantDefinition: class ConstantDefinition {
214+
$ConstantDefinition: class {
215215
constructor(type, name) {
216216
this.type = type;
217217
this.name = name;
@@ -221,7 +221,7 @@ var LibraryEmbind = {
221221
out.push(` ${this.name}: ${nameMap(this.type)};\n`);
222222
}
223223
},
224-
$EnumDefinition: class EnumDefinition {
224+
$EnumDefinition: class {
225225
constructor(typeId, name) {
226226
this.typeId = typeId;
227227
this.name = name;
@@ -255,7 +255,7 @@ var LibraryEmbind = {
255255
out.push('};\n');
256256
}
257257
},
258-
$ValueArrayDefinition: class ValueArrayDefinition {
258+
$ValueArrayDefinition: class {
259259
constructor(typeId, name) {
260260
this.typeId = typeId;
261261
this.name = name;
@@ -274,7 +274,7 @@ var LibraryEmbind = {
274274
out.push(' ];\n\n');
275275
}
276276
},
277-
$ValueObjectDefinition: class ValueObjectDefinition {
277+
$ValueObjectDefinition: class {
278278
constructor(typeId, name) {
279279
this.typeId = typeId;
280280
this.name = name;
@@ -294,7 +294,7 @@ var LibraryEmbind = {
294294
out.push('\n};\n\n');
295295
}
296296
},
297-
$TsPrinter: class TsPrinter {
297+
$TsPrinter: class {
298298
constructor(definitions) {
299299
this.definitions = definitions;
300300
const jsString = 'ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string';
@@ -360,7 +360,7 @@ var LibraryEmbind = {
360360
}
361361
},
362362

363-
$JsPrinter: class JsPrinter {
363+
$JsPrinter: class {
364364
constructor(definitions) {
365365
this.definitions = definitions;
366366
}

src/embind/emval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var LibraryEmVal = {
3232
{value: true},
3333
{value: false},
3434
);
35-
emval_handles.reserved = emval_handles.allocated.length
35+
Object.assign(emval_handles, /** @lends {emval_handles} */ { reserved: emval_handles.allocated.length }),
3636
Module['count_emval_handles'] = count_emval_handles;
3737
},
3838

src/jsifier.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ function(${args}) {
513513
if (contentText.match(/^\s*([^}]*)\s*=>/s)) {
514514
// Handle arrow functions
515515
contentText = `var ${mangled} = ` + contentText + ';';
516+
} else if (contentText.startsWith('class ')) {
517+
contentText = contentText.replace(/^class /, `class ${mangled} `);
516518
} else {
517519
// Handle regular (non-arrow) functions
518520
contentText = contentText.replace(/function(?:\s+([^(]+))?\s*\(/, `function ${mangled}(`);

src/library.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,41 +3623,37 @@ addToLibrary({
36233623
#endif
36243624
},
36253625

3626-
$handleAllocatorInit: function() {
3627-
Object.assign(HandleAllocator.prototype, /** @lends {HandleAllocator.prototype} */ {
3628-
get(id) {
3629-
#if ASSERTIONS
3630-
assert(this.allocated[id] !== undefined, `invalid handle: ${id}`);
3631-
#endif
3632-
return this.allocated[id];
3633-
},
3634-
has(id) {
3635-
return this.allocated[id] !== undefined;
3636-
},
3637-
allocate(handle) {
3638-
var id = this.freelist.pop() || this.allocated.length;
3639-
this.allocated[id] = handle;
3640-
return id;
3641-
},
3642-
free(id) {
3643-
#if ASSERTIONS
3644-
assert(this.allocated[id] !== undefined);
3645-
#endif
3646-
// Set the slot to `undefined` rather than using `delete` here since
3647-
// apparently arrays with holes in them can be less efficient.
3648-
this.allocated[id] = undefined;
3649-
this.freelist.push(id);
3650-
}
3651-
});
3652-
},
3653-
3654-
$HandleAllocator__postset: 'handleAllocatorInit()',
3655-
$HandleAllocator__deps: ['$handleAllocatorInit'],
3656-
$HandleAllocator__docs: '/** @constructor */',
3657-
$HandleAllocator: function() {
3658-
// Reserve slot 0 so that 0 is always an invalid handle
3659-
this.allocated = [undefined];
3660-
this.freelist = [];
3626+
$HandleAllocator: class {
3627+
constructor() {
3628+
// TODO(sbc): Use class fields once we allow/enable es2022 in
3629+
// JavaScript input to acorn and closure.
3630+
// Reserve slot 0 so that 0 is always an invalid handle
3631+
this.allocated = [undefined];
3632+
this.freelist = [];
3633+
}
3634+
get(id) {
3635+
#if ASSERTIONS
3636+
assert(this.allocated[id] !== undefined, `invalid handle: ${id}`);
3637+
#endif
3638+
return this.allocated[id];
3639+
};
3640+
has(id) {
3641+
return this.allocated[id] !== undefined;
3642+
};
3643+
allocate(handle) {
3644+
var id = this.freelist.pop() || this.allocated.length;
3645+
this.allocated[id] = handle;
3646+
return id;
3647+
};
3648+
free(id) {
3649+
#if ASSERTIONS
3650+
assert(this.allocated[id] !== undefined);
3651+
#endif
3652+
// Set the slot to `undefined` rather than using `delete` here since
3653+
// apparently arrays with holes in them can be less efficient.
3654+
this.allocated[id] = undefined;
3655+
this.freelist.push(id);
3656+
};
36613657
},
36623658

36633659
$getNativeTypeSize__deps: ['$POINTER_SIZE'],

src/library_exceptions.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,55 @@ var LibraryExceptions = {
2020
// reference counter) is not protected from that. Also protection is not enough, separate state
2121
// should be allocated. libcxxabi has concept of dependent exception which is used for that
2222
// purpose, it references the primary exception.
23-
//
24-
// excPtr - Thrown object pointer to wrap. Metadata pointer is calculated from it.
25-
$ExceptionInfo__docs: '/** @constructor */',
2623
$ExceptionInfo__deps: [
2724
'__cxa_is_pointer_type',
2825
#if EXCEPTION_DEBUG
2926
'$ptrToString'
3027
#endif
3128
],
32-
$ExceptionInfo: function(excPtr) {
33-
this.excPtr = excPtr;
34-
this.ptr = excPtr - {{{ C_STRUCTS.__cxa_exception.__size__ }}};
29+
$ExceptionInfo: class {
30+
// excPtr - Thrown object pointer to wrap. Metadata pointer is calculated from it.
31+
constructor(excPtr) {
32+
this.excPtr = excPtr;
33+
this.ptr = excPtr - {{{ C_STRUCTS.__cxa_exception.__size__ }}};
34+
}
3535

36-
this.set_type = function(type) {
36+
set_type(type) {
3737
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionType, 'type', '*') }}};
38-
};
38+
}
3939

40-
this.get_type = function() {
40+
get_type() {
4141
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionType, '*') }}};
42-
};
42+
}
4343

44-
this.set_destructor = function(destructor) {
44+
set_destructor(destructor) {
4545
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionDestructor, 'destructor', '*') }}};
46-
};
46+
}
4747

48-
this.get_destructor = function() {
48+
get_destructor() {
4949
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.exceptionDestructor, '*') }}};
50-
};
50+
}
5151

52-
this.set_caught = function(caught) {
52+
set_caught(caught) {
5353
caught = caught ? 1 : 0;
5454
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.caught, 'caught', 'i8') }}};
55-
};
55+
}
5656

57-
this.get_caught = function() {
57+
get_caught() {
5858
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.caught, 'i8') }}} != 0;
59-
};
59+
}
6060

61-
this.set_rethrown = function(rethrown) {
61+
set_rethrown(rethrown) {
6262
rethrown = rethrown ? 1 : 0;
6363
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.rethrown, 'rethrown', 'i8') }}};
64-
};
64+
}
6565

66-
this.get_rethrown = function() {
66+
get_rethrown() {
6767
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.rethrown, 'i8') }}} != 0;
68-
};
68+
}
6969

7070
// Initialize native structure fields. Should be called once after allocated.
71-
this.init = function(type, destructor) {
71+
init(type, destructor) {
7272
#if EXCEPTION_DEBUG
7373
dbg('ExceptionInfo init: ' + [type, destructor]);
7474
#endif
@@ -77,19 +77,19 @@ var LibraryExceptions = {
7777
this.set_destructor(destructor);
7878
}
7979

80-
this.set_adjusted_ptr = function(adjustedPtr) {
80+
set_adjusted_ptr(adjustedPtr) {
8181
{{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.adjustedPtr, 'adjustedPtr', '*') }}};
82-
};
82+
}
8383

84-
this.get_adjusted_ptr = function() {
84+
get_adjusted_ptr() {
8585
return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.adjustedPtr, '*') }}};
86-
};
86+
}
8787

8888
// Get pointer which is expected to be received by catch clause in C++ code. It may be adjusted
8989
// when the pointer is casted to some of the exception object base classes (e.g. when virtual
9090
// inheritance is used). When a pointer is thrown this method should return the thrown pointer
9191
// itself.
92-
this.get_exception_ptr = function() {
92+
get_exception_ptr() {
9393
// Work around a fastcomp bug, this code is still included for some reason in a build without
9494
// exceptions support.
9595
var isPointer = ___cxa_is_pointer_type(this.get_type());
@@ -99,7 +99,7 @@ var LibraryExceptions = {
9999
var adjusted = this.get_adjusted_ptr();
100100
if (adjusted !== 0) return adjusted;
101101
return this.excPtr;
102-
};
102+
}
103103
},
104104

105105
// Here, we throw an exception after recording a couple of values that we need to remember

src/library_wasmfs_opfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ addToLibrary({
1616

1717
#if !PTHREADS
1818
// OPFS will only be used on modern browsers that supports JS classes.
19-
$FileSystemAsyncAccessHandle: class FileSystemAsyncAccessHandle {
19+
$FileSystemAsyncAccessHandle: class {
2020
// This class implements the same interface as the sync version, but has
2121
// async reads and writes. Hopefully this will one day be implemented by the
2222
// platform so we can remove it.

test/code_size/embind_val_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 673,
33
"a.html.gz": 431,
4-
"a.js": 7468,
5-
"a.js.gz": 3137,
4+
"a.js": 7426,
5+
"a.js.gz": 3122,
66
"a.wasm": 11458,
77
"a.wasm.gz": 5733,
8-
"total": 19599,
9-
"total_gz": 9301
8+
"total": 19557,
9+
"total_gz": 9286
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10012
1+
9963
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24846
1+
24728
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9995
1+
9946
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24814
1+
24696
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11122
1+
11038
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28976
1+
28618
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11129
1+
11045
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28976
1+
28618
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10012
1+
9963
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24846
1+
24728
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5308
1+
5281
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12296
1+
12184
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8454
1+
8449
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
23173
1+
23153
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7282
1+
7278
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19797
1+
19777
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58433
1+
58408

0 commit comments

Comments
 (0)