From 1abb2a43fe09c9b5f7a525386b2aa434b790a671 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 9 Oct 2024 11:57:51 -0700 Subject: [PATCH] [embind] Use ES6 class for ClassHandle. NFC --- src/embind/embind.js | 156 ++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 82 deletions(-) diff --git a/src/embind/embind.js b/src/embind/embind.js index d547b471afc23..fced8247004ad 100644 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -1574,7 +1574,8 @@ var LibraryEmbind = { })); }, - $init_ClassHandle__deps: [ + // root of all pointer and smart pointer handles in embind + $ClassHandle__deps: [ '$ClassHandle', '$shallowCopyInternalPointer', '$throwInstanceAlreadyDeleted', @@ -1583,100 +1584,91 @@ var LibraryEmbind = { '$throwBindingError', '$detachFinalizer', ], - $init_ClassHandle: () => { - Object.assign(ClassHandle.prototype, { - "isAliasOf"(other) { - if (!(this instanceof ClassHandle)) { - return false; - } - if (!(other instanceof ClassHandle)) { - return false; - } - - var leftClass = this.$$.ptrType.registeredClass; - var left = this.$$.ptr; - other.$$ = /** @type {Object} */ (other.$$); - var rightClass = other.$$.ptrType.registeredClass; - var right = other.$$.ptr; + $ClassHandle: class { + "isAliasOf"(other) { + if (!(this instanceof ClassHandle)) { + return false; + } + if (!(other instanceof ClassHandle)) { + return false; + } - while (leftClass.baseClass) { - left = leftClass.upcast(left); - leftClass = leftClass.baseClass; - } + var leftClass = this.$$.ptrType.registeredClass; + var left = this.$$.ptr; + other.$$ = /** @type {Object} */ (other.$$); + var rightClass = other.$$.ptrType.registeredClass; + var right = other.$$.ptr; - while (rightClass.baseClass) { - right = rightClass.upcast(right); - rightClass = rightClass.baseClass; - } + while (leftClass.baseClass) { + left = leftClass.upcast(left); + leftClass = leftClass.baseClass; + } - return leftClass === rightClass && left === right; - }, + while (rightClass.baseClass) { + right = rightClass.upcast(right); + rightClass = rightClass.baseClass; + } - "clone"() { - if (!this.$$.ptr) { - throwInstanceAlreadyDeleted(this); - } + return leftClass === rightClass && left === right; + } - if (this.$$.preservePointerOnDelete) { - this.$$.count.value += 1; - return this; - } else { - var clone = attachFinalizer(Object.create(Object.getPrototypeOf(this), { - $$: { - value: shallowCopyInternalPointer(this.$$), - } - })); + "clone"() { + if (!this.$$.ptr) { + throwInstanceAlreadyDeleted(this); + } - clone.$$.count.value += 1; - clone.$$.deleteScheduled = false; - return clone; - } - }, + if (this.$$.preservePointerOnDelete) { + this.$$.count.value += 1; + return this; + } else { + var clone = attachFinalizer(Object.create(Object.getPrototypeOf(this), { + $$: { + value: shallowCopyInternalPointer(this.$$), + } + })); - "delete"() { - if (!this.$$.ptr) { - throwInstanceAlreadyDeleted(this); - } + clone.$$.count.value += 1; + clone.$$.deleteScheduled = false; + return clone; + } + } - if (this.$$.deleteScheduled && !this.$$.preservePointerOnDelete) { - throwBindingError('Object already scheduled for deletion'); - } + "delete"() { + if (!this.$$.ptr) { + throwInstanceAlreadyDeleted(this); + } - detachFinalizer(this); - releaseClassHandle(this.$$); + if (this.$$.deleteScheduled && !this.$$.preservePointerOnDelete) { + throwBindingError('Object already scheduled for deletion'); + } - if (!this.$$.preservePointerOnDelete) { - this.$$.smartPtr = undefined; - this.$$.ptr = undefined; - } - }, + detachFinalizer(this); + releaseClassHandle(this.$$); - "isDeleted"() { - return !this.$$.ptr; - }, + if (!this.$$.preservePointerOnDelete) { + this.$$.smartPtr = undefined; + this.$$.ptr = undefined; + } + } - "deleteLater"() { - if (!this.$$.ptr) { - throwInstanceAlreadyDeleted(this); - } - if (this.$$.deleteScheduled && !this.$$.preservePointerOnDelete) { - throwBindingError('Object already scheduled for deletion'); - } - deletionQueue.push(this); - if (deletionQueue.length === 1 && delayFunction) { - delayFunction(flushPendingDeletes); - } - this.$$.deleteScheduled = true; - return this; - }, - }); - }, + "isDeleted"() { + return !this.$$.ptr; + } - $ClassHandle__docs: '/** @constructor */', - $ClassHandle__deps: ['$init_ClassHandle'], - $ClassHandle__postset: 'init_ClassHandle()', - // root of all pointer and smart pointer handles in embind - $ClassHandle: function() { + "deleteLater"() { + if (!this.$$.ptr) { + throwInstanceAlreadyDeleted(this); + } + if (this.$$.deleteScheduled && !this.$$.preservePointerOnDelete) { + throwBindingError('Object already scheduled for deletion'); + } + deletionQueue.push(this); + if (deletionQueue.length === 1 && delayFunction) { + delayFunction(flushPendingDeletes); + } + this.$$.deleteScheduled = true; + return this; + } }, $throwInstanceAlreadyDeleted__deps: ['$throwBindingError'],