-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce duplication between PropertyReadExprNode and ArrayIndexReadExp…
…rNode by introducing ObjectPropertyReadNode.
- Loading branch information
Showing
9 changed files
with
224 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...java/com/endoflineblog/truffle/part_11/nodes/exprs/properties/ObjectPropertyReadNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.endoflineblog.truffle.part_11.nodes.exprs.properties; | ||
|
||
import com.endoflineblog.truffle.part_11.exceptions.EasyScriptException; | ||
import com.endoflineblog.truffle.part_11.nodes.exprs.strings.ReadTruffleStringPropertyNode; | ||
import com.endoflineblog.truffle.part_11.runtime.Undefined; | ||
import com.oracle.truffle.api.dsl.Cached; | ||
import com.oracle.truffle.api.dsl.Fallback; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import com.oracle.truffle.api.interop.UnknownIdentifierException; | ||
import com.oracle.truffle.api.interop.UnsupportedMessageException; | ||
import com.oracle.truffle.api.library.CachedLibrary; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.strings.TruffleString; | ||
|
||
/** | ||
* A Node for reading a property of a JavaScript object. | ||
* Used by {@link PropertyReadExprNode} and {@link com.endoflineblog.truffle.part_11.nodes.exprs.arrays.ArrayIndexReadExprNode}. | ||
*/ | ||
public abstract class ObjectPropertyReadNode extends Node { | ||
public abstract Object executePropertyRead(Object target, Object property); | ||
|
||
/** | ||
* The specialization for reading a property of a {@link TruffleString}. | ||
* Simply delegates to {@link ReadTruffleStringPropertyNode}. | ||
*/ | ||
@Specialization | ||
protected Object readPropertyOfString(TruffleString target, Object property, | ||
@Cached ReadTruffleStringPropertyNode readStringPropertyNode) { | ||
return readStringPropertyNode.executeReadTruffleStringProperty( | ||
target, property); | ||
} | ||
|
||
@Specialization(guards = "interopLibrary.hasMembers(target)", limit = "1") | ||
protected Object readProperty(Object target, String propertyName, | ||
@CachedLibrary("target") InteropLibrary interopLibrary) { | ||
try { | ||
return interopLibrary.readMember(target, propertyName); | ||
} catch (UnknownIdentifierException e) { | ||
return Undefined.INSTANCE; | ||
} catch (UnsupportedMessageException e) { | ||
throw new EasyScriptException(this, e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Reading any property of {@code undefined} | ||
* results in an error in JavaScript. | ||
*/ | ||
@Specialization(guards = "interopLibrary.isNull(target)", limit = "1") | ||
protected Object readPropertyOfUndefined(@SuppressWarnings("unused") Object target, Object property, | ||
@CachedLibrary("target") @SuppressWarnings("unused") InteropLibrary interopLibrary) { | ||
throw new EasyScriptException("Cannot read properties of undefined (reading '" + property + "')"); | ||
} | ||
|
||
/** | ||
* Accessing a property of anything that is not {@code undefined} | ||
* but doesn't have any members returns simply {@code undefined} | ||
* in JavaScript. | ||
*/ | ||
@Fallback | ||
protected Object readPropertyOfNonUndefinedWithoutMembers(@SuppressWarnings("unused") Object target, | ||
@SuppressWarnings("unused") Object property) { | ||
return Undefined.INSTANCE; | ||
} | ||
} |
Oops, something went wrong.