Skip to content

Commit

Permalink
Remove @observe, add @Property 'change' (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
iccir committed Mar 8, 2023
1 parent 5c03ef8 commit fb58a48
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 258 deletions.
149 changes: 11 additions & 138 deletions ext/esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,9 @@ return /******/ (function(modules) { // webpackBootstrap
NSAnyExpression: 'NSAnyExpression',
NSCastExpression: 'NSCastExpression',
NSTypeAnnotation: 'NSTypeAnnotation',
NSEachStatement: 'NSEachStatement',
NSGlobalDeclaration: 'NSGlobalDeclaration',
NSBridgedDeclaration: 'NSBridgedDeclaration',
NSTypeDefinition: 'NSTypeDefinition',
NSObserveDirective: 'NSObserveDirective',
NSObserveAttribute: 'NSObserveAttribute'
NSTypeDefinition: 'NSTypeDefinition'
//!ns: end changes
};

Expand Down Expand Up @@ -1944,12 +1941,11 @@ return /******/ (function(modules) { // webpackBootstrap
}());
exports.NSMethodNameSegment = NSMethodNameSegment;
var NSClassImplementation = /** @class */ (function () {
function NSClassImplementation(id, inheritanceList, category, body) {
function NSClassImplementation(id, inheritanceList, body) {
this.type = syntax_1.Syntax.NSClassImplementation;
this.id = id;
this.body = body;
this.inheritanceList = inheritanceList;
this.category = category;
}
return NSClassImplementation;
}());
Expand Down Expand Up @@ -2018,24 +2014,6 @@ return /******/ (function(modules) { // webpackBootstrap
return NSPropertyAttribute;
}());
exports.NSPropertyAttribute = NSPropertyAttribute;
var NSObserveDirective = /** @class */ (function () {
function NSObserveDirective(ids, attributes) {
this.type = syntax_1.Syntax.NSObserveDirective;
this.ids = ids;
this.attributes = attributes;
}
return NSObserveDirective;
}());
exports.NSObserveDirective = NSObserveDirective;
var NSObserveAttribute = /** @class */ (function () {
function NSObserveAttribute(name, selector) {
this.type = syntax_1.Syntax.NSObserveAttribute;
this.name = name;
this.selector = selector;
}
return NSObserveAttribute;
}());
exports.NSObserveAttribute = NSObserveAttribute;
var NSSelectorDirective = /** @class */ (function () {
function NSSelectorDirective(name) {
this.type = syntax_1.Syntax.NSSelectorDirective;
Expand Down Expand Up @@ -2120,16 +2098,6 @@ return /******/ (function(modules) { // webpackBootstrap
return NSTypeDefinition;
}());
exports.NSTypeDefinition = NSTypeDefinition;
var NSEachStatement = /** @class */ (function () {
function NSEachStatement(left, right, body) {
this.type = syntax_1.Syntax.NSEachStatement;
this.left = left;
this.right = right;
this.body = body;
}
return NSEachStatement;
}());
exports.NSEachStatement = NSEachStatement;
var NSGlobalDeclaration = /** @class */ (function () {
function NSGlobalDeclaration(declaration, declarators) {
this.type = syntax_1.Syntax.NSGlobalDeclaration;
Expand Down Expand Up @@ -2245,8 +2213,7 @@ return /******/ (function(modules) { // webpackBootstrap
inSwitch: false,
inClassConstructor: false,
labelSet: {},
strict: false,
ns_inImplementation: false //!ns: Add ns_inImplementation
strict: false
};
this.tokens = [];
this.startMarker = {
Expand Down Expand Up @@ -4755,9 +4722,6 @@ return /******/ (function(modules) { // webpackBootstrap
case '@enum':
statement = this.ns_parseEnumStatement();
break;
case '@each':
statement = this.ns_parseEachStatement();
break;
case '@global':
statement = this.ns_parseGlobalDeclaration();
break;
Expand Down Expand Up @@ -5644,7 +5608,7 @@ return /******/ (function(modules) { // webpackBootstrap
var startToken = this.lookahead;
var name = this.parseIdentifierName().name;
var selector = null;
if (name == 'getter' || name == 'setter') {
if (name == 'getter' || name == 'setter' || name == 'change') {
this.expect('=');
selector = this.ns_parseSelector(true);
}
Expand All @@ -5667,41 +5631,6 @@ return /******/ (function(modules) { // webpackBootstrap
this.consumeSemicolon();
return this.finalize(node, new Node.NSPropertyDirective(identifier, attributes));
};
Parser.prototype.ns_parseObserveAttribute = function () {
var node = this.createNode();
var startToken = this.lookahead;
var name = this.parseVariableIdentifier().name;
var selector = null;
var allowedNames = ['change', 'set'];
if (name == 'before' || name == 'after') {
this.expect('=');
selector = this.ns_parseSelector(true);
}
else if (allowedNames.indexOf(name) < 0) {
this.throwUnexpectedToken(startToken);
}
return this.finalize(node, new Node.NSObserveAttribute(name, selector));
};
Parser.prototype.ns_parseObserveDirective = function () {
var node = this.createNode();
var attributes = [];
var ids = [];
this.expectKeyword('@observe');
this.expect('(');
attributes.push(this.ns_parseObserveAttribute());
while (this.match(',')) {
this.expect(',');
attributes.push(this.ns_parseObserveAttribute());
}
this.expect(')');
ids.push(this.parseVariableIdentifier());
while (this.match(',')) {
this.expect(',');
ids.push(this.parseVariableIdentifier());
}
this.consumeSemicolon();
return this.finalize(node, new Node.NSObserveDirective(ids, attributes));
};
// This should be called when lookahead is a '<' token.
Parser.prototype.ns_parseTypeAngleSuffix = function () {
var parser = this;
Expand Down Expand Up @@ -5862,41 +5791,24 @@ return /******/ (function(modules) { // webpackBootstrap
if (this.matchKeyword('@end')) {
break;
}
else if (token.type === 4 /* Keyword */) {
switch (token.value) {
case '@property':
sourceElement = this.ns_parsePropertyDirective();
break;
case '@observe':
sourceElement = this.ns_parseObserveDirective();
break;
default:
sourceElement = this.parseStatementListItem();
break;
}
else if (this.matchKeyword('@property')) {
sourceElement = this.ns_parsePropertyDirective();
}
else if (this.match('-') || this.match('+')) {
sourceElement = this.ns_parseMethodDefinition();
}
else {
sourceElement = this.parseStatementListItem();
this.throwUnexpectedToken(token);
}
if (typeof sourceElement === 'undefined') {
break;
if (sourceElement) {
sourceElements.push(sourceElement);
}
sourceElements.push(sourceElement);
}
return this.finalize(node, new Node.BlockStatement(sourceElements));
};
Parser.prototype.ns_parseClassImplementationDefinition = function () {
var node = this.createNode();
var inheritanceList = null;
var extension = false;
var category = null;
if (this.context.ns_inImplementation) {
this.throwError(messages_1.Messages.NSCannotNestImplementations);
}
this.context.ns_inImplementation = true;
var oldLabelSet = this.context.labelSet;
var previousStrict = this.context.strict;
this.context.strict = true;
Expand All @@ -5905,19 +5817,12 @@ return /******/ (function(modules) { // webpackBootstrap
// Has inheritance list
if (this.match(':')) {
inheritanceList = this.ns_parseInheritanceList();
// Category on existing class
}
else if (this.match('(')) {
this.expect('(');
category = this.parseVariableIdentifier();
this.expect(')');
}
var body = this.ns_parseClassImplementationBody();
this.expectKeyword('@end');
this.context.strict = previousStrict;
this.context.ns_inImplementation = false;
this.context.labelSet = oldLabelSet;
return this.finalize(node, new Node.NSClassImplementation(id, inheritanceList, category, body));
return this.finalize(node, new Node.NSClassImplementation(id, inheritanceList, body));
};
Parser.prototype.ns_parseInheritanceList = function () {
var node = this.createNode();
Expand Down Expand Up @@ -5968,13 +5873,9 @@ return /******/ (function(modules) { // webpackBootstrap
Parser.prototype.ns_parseProtocolDefinition = function () {
var node = this.createNode();
var inheritanceList = null;
if (this.context.ns_inImplementation) {
this.throwError(messages_1.Messages.NSCannotNestImplementations);
}
var oldLabelSet = this.context.labelSet;
var previousStrict = this.context.strict;
this.context.strict = true;
this.context.ns_inImplementation = true;
this.expectKeyword('@protocol');
var id = this.parseVariableIdentifier();
if (this.match(':')) {
Expand All @@ -5983,7 +5884,6 @@ return /******/ (function(modules) { // webpackBootstrap
var body = this.ns_parseProtocolDefinitionBody();
this.expectKeyword('@end');
this.context.strict = previousStrict;
this.context.ns_inImplementation = false;
this.context.labelSet = oldLabelSet;
return this.finalize(node, new Node.NSProtocolDefinition(id, inheritanceList, body));
};
Expand Down Expand Up @@ -6259,32 +6159,6 @@ return /******/ (function(modules) { // webpackBootstrap
return this.finalize(node, new Node.Identifier(token.value));
}
};
Parser.prototype.ns_parseEachStatement = function () {
var node = this.createNode();
var left;
this.expectKeyword('@each');
this.expect('(');
if (this.matchKeyword('var') || this.matchKeyword('let')) {
var marker = this.createNode();
var kind = this.nextToken().value;
var previousAllowIn = this.context.allowIn;
this.context.allowIn = false;
var declarations = this.parseBindingList(kind, { inFor: false });
left = this.finalize(marker, new Node.VariableDeclaration(declarations, kind));
this.context.allowIn = previousAllowIn;
}
else {
left = this.parseVariableIdentifier();
}
this.expectKeyword('in');
var right = this.parseExpression();
this.expect(')');
var oldInIteration = this.context.inIteration;
this.context.inIteration = true;
var body = this.parseStatement();
this.context.inIteration = oldInIteration;
return this.finalize(node, new Node.NSEachStatement(left, right, body));
};
Parser.prototype.ns_parseGlobalDeclaration = function () {
var node = this.createNode();
this.expectKeyword('@global');
Expand Down Expand Up @@ -6775,8 +6649,7 @@ return /******/ (function(modules) { // webpackBootstrap
(id === '@each') ||
(id === '@global') ||
(id === '@type') ||
(id === '@bridged') ||
(id === '@observe');
(id === '@bridged');
}
//!ns: end changes
switch (id.length) {
Expand Down
31 changes: 7 additions & 24 deletions src/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ build()
let setterEnabled = true;
let setterCopies = false;

let changeName = null;

for (let i = 0, length = node.attributes.length; i < length; i++) {
let attribute = node.attributes[i];
let attributeName = attribute.name;
Expand Down Expand Up @@ -211,6 +213,9 @@ build()
} else if (attributeName == "setter") {
setterName = attribute.selector.selectorName;

} else if (attributeName == "change") {
changeName = attribute.selector.selectorName;

} else if (attributeName == "class") {
Utils.throwError(NSError.NotYetSupported, "'class' attribute is not supported", node);

Expand All @@ -230,7 +235,8 @@ build()

let setter = setterEnabled ? {
name: setterName || ("set" + name[0].toUpperCase() + name.slice(1) + ":"),
copies: setterCopies
copies: setterCopies,
change: changeName
} : null;

let property = new Model.NSProperty(makeLocation(node), name, type, "_" + name, getter, setter);
Expand All @@ -242,26 +248,6 @@ build()
}
}

function handleNSObserveDirective(node)
{
let after = null;

for (let i = 0, length = node.attributes.length; i < length; i++) {
let attribute = node.attributes[i];

if (attribute.name == "after") {
after = attribute.selector.selectorName;
}
}

for (let i = 0, length = node.ids.length; i < length; i++) {
let name = node.ids[i].name;

let observer = new Model.NSObserver(makeLocation(node), name, after);
if (currentClass) currentClass.addObserver(observer);
}
}

function handleNSTypeDefinition(node)
{
let name = node.name;
Expand Down Expand Up @@ -464,9 +450,6 @@ build()
} else if (type === Syntax.NSPropertyDirective) {
handleNSPropertyDirective(node);

} else if (type === Syntax.NSObserveDirective) {
handleNSObserveDirective(node);

} else if (type === Syntax.NSTypeDefinition) {
handleNSTypeDefinition(node);

Expand Down
24 changes: 10 additions & 14 deletions src/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,32 +769,28 @@ generate()
let result = "";
if (setterMethod?.synthesized) {
if (language === LanguageEcmascript5) {
let observers = currentClass.getObserversWithName(name) || [ ];
let changeName = property.setter.change;

let s = [ ];

if (observers.length > 0) {
s.push( "var old = " + ivar + ";" );
s.push("if (old !== arg) {");
if (changeName) {
s.push(`if (${ivar} !== arg) {`);
}

if (property.setter.copies) {
s.push(ivar + " = " + NSRootVariable + ".makeCopy(arg);");
s.push(`${ivar} = ${NSRootVariable}.makeCopy(arg);`);
} else {
s.push(ivar + " = arg;");
s.push(`${ivar} = arg;`);
}

if (observers.length > 0) {
_.each(observers, observer => {
let after = observer.after && symbolTyper.getSymbolForSelectorName(observer.after);
if (after) s.push( "this." + after + "(old);" );
});

s.push("}");
if (changeName) {
s.push(`this.${symbolTyper.getSymbolForSelectorName(changeName)}();`);
s.push(`}`);
}

let symbol = symbolTyper.getSymbolForSelectorName(property.setter.name);

result += symbol + "(arg) {" + s.join(" ") + " } ";
result += `${symbol}(arg) {${s.join(" ")} } `;
}
}

Expand Down
Loading

0 comments on commit fb58a48

Please sign in to comment.