Skip to content

Commit

Permalink
Directly use Og syscall to optimise some property accessor uses
Browse files Browse the repository at this point in the history
Properties with descriptors (setters/getters) and builtin properties will still be looked up via `#core:getProperty`.
  • Loading branch information
James-Livesey committed Aug 18, 2024
1 parent 87e5754 commit 1597572
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
11 changes: 7 additions & 4 deletions dist/libvoxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -1468,10 +1468,13 @@ void voxel_builtins_core_getObjectItem(voxel_Executor* executor) {
voxel_Thing* key = voxel_pop(executor);
voxel_Thing* object = voxel_pop(executor);

VOXEL_REQUIRE(
key &&
VOXEL_ARG(object, VOXEL_TYPE_OBJECT)
);
if (object->type != VOXEL_TYPE_OBJECT) {
voxel_pushNull(executor);

goto voxel_finally;
}

VOXEL_REQUIRE(key);

voxel_ObjectItem* objectItem = voxel_getObjectItem(object, key);

Expand Down
11 changes: 7 additions & 4 deletions src/builtins/core/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ void voxel_builtins_core_getObjectItem(voxel_Executor* executor) {
voxel_Thing* key = voxel_pop(executor);
voxel_Thing* object = voxel_pop(executor);

VOXEL_REQUIRE(
key &&
VOXEL_ARG(object, VOXEL_TYPE_OBJECT)
);
if (object->type != VOXEL_TYPE_OBJECT) {
voxel_pushNull(executor);

goto voxel_finally;
}

VOXEL_REQUIRE(key);

voxel_ObjectItem* objectItem = voxel_getObjectItem(object, key);

Expand Down
8 changes: 7 additions & 1 deletion tools/vxbuild-js/coreparts.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,10 @@ builtinProps["W"]["deref"] = `
return function() {
return syscall Wd(thing);
};
`;
`;

export var builtinPropNames = ["weak"];

Object.keys(builtinProps).forEach(function(type) {
Object.keys(builtinProps[type]).forEach((property) => builtinPropNames.push(property));
});
20 changes: 20 additions & 0 deletions tools/vxbuild-js/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as dce from "./dce.js";
import * as parser from "./parser.js";
import * as statements from "./statements.js";
import * as staticMacros from "./staticmacros.js";
import * as coreParts from "./coreparts.js";

export const ALL_BINARY_OPERATOR_CODE = {
"*": codeGen.join(
Expand Down Expand Up @@ -987,6 +988,10 @@ export class ClassNode extends ast.AstNode {
instance.propertySymbols.push(namespaces.Symbol.generateForProperty(propertyToken.value, retain));
instance.propertyDescriptorTypes.push(descriptorType);

if (descriptorType != null) {
namespaces.descriptorPropertyNames.push(propertyToken.value);
}

if (this.maybeEat(tokens, [new ast.TokenQuery(tokeniser.AssignmentOperatorToken, "=")])) {
instance.expectChildByMatching(tokens, [ExpressionNode], namespace);
} else {
Expand Down Expand Up @@ -1431,6 +1436,21 @@ export class PropertyAccessorNode extends ast.AstNode {
return enumValue != null ? codeGen.number(enumValue) : codeGen.bytes(codeGen.vxcTokens.NULL);
}

if (
!namespaces.descriptorPropertyNames.includes(this.propertySymbol.name) &&
!coreParts.builtinPropNames.includes(this.propertySymbol.name)
) {
return codeGen.join(
codeGen.bytes(codeGen.vxcTokens.DUPE),
codeGen.number(1),
codeGen.systemCall("Ms"),
codeGen.bytes(codeGen.vxcTokens.POP),
this.propertySymbol.generateCode(options),
codeGen.number(2),
codeGen.systemCall("Og")
);
}

return codeGen.join(
this.propertySymbol.generateCode(options),
codeGen.number(2),
Expand Down
1 change: 1 addition & 0 deletions tools/vxbuild-js/namespaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export var coreNamespace = null;
export var propertySymbols = {};
export var propertySymbolRetainedNames = {};
export var propertySymbolUses = [];
export var descriptorPropertyNames = [];

export class Namespace {
constructor(sourceContainer = null, preferredId = null) {
Expand Down

0 comments on commit 1597572

Please sign in to comment.