Skip to content

Commit

Permalink
Implements #168
Browse files Browse the repository at this point in the history
  • Loading branch information
iccir committed Mar 14, 2023
1 parent aa94429 commit 5c21ed7
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 209 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ NilScript was formerly known as oj.
- [Behind the Scenes](#class-compiler)
- [The Built-in Base Class](#base-class)
- [Methods](#method)
- [Falsy Messaging](#method-falsy)
- [Nullish Messaging](#method-nullish)
- [Behind the Scenes](#method-compiler)
- [Properties and Instance Variables](#property)
- [Synthesis](#property-synthesis)
Expand Down Expand Up @@ -196,15 +196,17 @@ Old-school bare method declarations may also be used:
@end
```

### <a name="method-falsy"></a>Falsy Messaging
### <a name="method-nullish"></a>Nullish Messaging

Just as Objective-C supports messaging `nil`, NilScript supports the concept of "Falsy Messaging".
Just as Objective-C supports messaging `nil`, NilScript supports the concept of "Nullish Messaging".

Any message to a falsy JavaScript value (false / undefined / null / 0 / "" / NaN ) will return that value.
Any message to a nullish JavaScript value (`undefined` or `null`) will return `null`.

```
let foo = null;
let result = [foo doSomething]; // result is null
let foo1 = null;
let foo2 = undefined;
let result1 = [foo1 doSomething]; // result1 is null
let result2 = [foo2 doSomething]; // result2 is also null
```

### <a name="method-compiler"></a>Behind the Scenes (Methods)
Expand Down
36 changes: 33 additions & 3 deletions src/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ build()

let traverser = new Traverser(nsFile.ast);

let currentClass, currentMethod;
let currentClass, currentMethod, currentMethodNode;
let currentProtocol;
let functionCount = 0;

let usedSelectorMap = { };

Expand Down Expand Up @@ -159,6 +160,8 @@ build()
let method = makeNSMethodNode(node);
currentClass.addMethod(method);
currentMethod = method;
currentMethodNode = node;
functionCount = 0;
}

function handleNSMethodDeclaration(node)
Expand Down Expand Up @@ -406,8 +409,25 @@ build()
let name = node.name;
let transformable = isIdentifierTransformable(node);

if (transformable && (name[0] == "_") && (name.length > 0) && currentMethod && currentClass) {
currentClass.markUsedIvar(name);
if (currentMethod && currentClass) {
if (transformable && (name[0] == "_") && (name.length > 0)) {
if (functionCount > 0) {
currentMethodNode.ns_needs_var_self = true;
}

currentClass.markUsedIvar(name);

} else if (name == "self") {
if (functionCount > 0) {
currentMethodNode.ns_needs_var_self = true;
}

let parent = node.ns_parent;

if (parent.type == Syntax.AssignmentExpression && parent.left == node) {
currentMethodNode.ns_needs_var_self = true;
}
}
}

node.ns_transformable = transformable;
Expand Down Expand Up @@ -475,9 +495,14 @@ build()
handleVariableDeclarator(node);

} else if (type === Syntax.FunctionDeclaration || type === Syntax.FunctionExpression) {
functionCount++;
handleFunctionDeclarationOrExpression(node);

} else if (type === Syntax.NSMessageExpression) {
if (node.selectorName == "alloc") {
node.ns_nonnull = true;
}

usedSelectorMap[node.selectorName] = true;

} else if (type === Syntax.NSSelectorDirective) {
Expand Down Expand Up @@ -505,12 +530,17 @@ build()
if (type === Syntax.NSClassImplementation) {
currentClass = null;
currentMethod = null;
currentMethodNode = null;

} else if (type === Syntax.NSProtocolDefinition) {
currentProtocol = null;

} else if (type === Syntax.NSMethodDefinition) {
currentMethod = null;
currentMethodNode = null;

} else if (type === Syntax.FunctionDeclaration || type === Syntax.FunctionExpression) {
functionCount--;
}
});

Expand Down
Loading

0 comments on commit 5c21ed7

Please sign in to comment.