Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lukas tiefenthaler #639

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;

import com.oracle.truffle.js.runtime.JSContextOptions;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.junit.Assert;
import org.junit.Test;

import com.oracle.truffle.js.test.JSTest;
Expand Down Expand Up @@ -120,4 +123,16 @@ public void testHostMethodStatic() {
}
}

}
@Test
public void testJavaTimeInstant() {
try (Context context = JSTest.newContextBuilder(ID).option(FOREIGN_OBJECT_PROTOTYPE_NAME, "true").allowHostAccess(HostAccess.ALL).allowHostClassLookup(s -> true).build()) {
java.time.Instant inst = Instant.ofEpochMilli(100_000_000);
Value method = context.eval(ID, "( (inst) => { return inst; } );");
assertTrue(method.canExecute());
Value result = method.execute(inst);
assertTrue(result.isInstant());
assertTrue(result.asInstant().equals(inst));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.strings.TruffleString;
Expand Down Expand Up @@ -101,6 +102,7 @@
import com.oracle.truffle.js.nodes.temporal.ToTemporalDurationNode;
import com.oracle.truffle.js.runtime.Boundaries;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.Strings;
Expand Down Expand Up @@ -232,6 +234,7 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
return null;
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalCalendarGetterNode extends JSBuiltinNode {

public final TemporalCalendarPrototype property;
Expand All @@ -241,6 +244,7 @@ public JSTemporalCalendarGetterNode(JSContext context, JSBuiltin builtin, Tempor
this.property = property;
}

@CompilerDirectives.TruffleBoundary
@Specialization(guards = "isJSTemporalCalendar(thisObj)")
protected Object durationGetter(Object thisObj,
@Cached JSToStringNode toStringNode) {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.js.builtins.JSBuiltinsContainer;
import com.oracle.truffle.js.builtins.temporal.TemporalPlainDatePrototypeBuiltins.JSTemporalBuiltinOperation;
Expand All @@ -71,13 +75,15 @@
import com.oracle.truffle.js.nodes.temporal.ToTemporalInstantNode;
import com.oracle.truffle.js.runtime.BigInt;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRealm;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
import com.oracle.truffle.js.runtime.builtins.temporal.JSTemporalInstant;
import com.oracle.truffle.js.runtime.builtins.temporal.JSTemporalInstantObject;
import com.oracle.truffle.js.runtime.builtins.temporal.JSTemporalPlainDateTimeObject;
import com.oracle.truffle.js.runtime.builtins.temporal.JSTemporalPlainTime;
import com.oracle.truffle.js.runtime.builtins.temporal.JSTemporalTimeZone;
import com.oracle.truffle.js.runtime.builtins.temporal.JSTemporalTimeZoneObject;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
Expand Down Expand Up @@ -158,6 +164,7 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
return null;
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetterNode extends JSBuiltinNode {

public final TemporalTimeZonePrototype property;
Expand All @@ -167,6 +174,7 @@ public JSTemporalTimeZoneGetterNode(JSContext context, JSBuiltin builtin, Tempor
this.property = property;
}

@CompilerDirectives.TruffleBoundary
@Specialization(guards = "isJSTemporalTimeZone(thisObj)")
protected TruffleString timeZoneGetter(Object thisObj,
@Cached JSToStringNode toStringNode) {
Expand All @@ -183,20 +191,35 @@ protected TruffleString timeZoneGetter(Object thisObj,
protected static int error(@SuppressWarnings("unused") Object thisObj) {
throw TemporalErrors.createTypeErrorTemporalTimeZoneExpected();
}

@CompilerDirectives.TruffleBoundary
public static JSTemporalTimeZoneObject javaTimeZoneToTimeZone(Object thisObj, InteropLibrary interop, JSContext ctx) {
try {
java.time.ZoneId zone = interop.asTimeZone(thisObj);
TruffleString identifier = TruffleString.fromJavaStringUncached(zone.getId(), TruffleString.Encoding.UTF_32);

return JSTemporalTimeZone.create(ctx, null, identifier);
} catch (UnsupportedMessageException e) {
return null;
}
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneToString extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneToString(JSContext context, JSBuiltin builtin) {
super(context, builtin);
}

@Specialization
protected TruffleString toString(Object thisObj) {
return requireTemporalTimeZone(thisObj).getIdentifier();
protected TruffleString toString(Object thisObj,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
return requireTemporalTimeZone(thisObj, interop, getContext()).getIdentifier();
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneToJSON extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneToJSON(JSContext context, JSBuiltin builtin) {
Expand All @@ -205,12 +228,14 @@ protected JSTemporalTimeZoneToJSON(JSContext context, JSBuiltin builtin) {

@Specialization
protected TruffleString toJSON(Object thisObj,
@Cached("create()") JSToStringNode toString) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached("create()") JSToStringNode toString,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
return toString.executeString(timeZone);
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneValueOf extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneValueOf(JSContext context, JSBuiltin builtin) {
Expand All @@ -223,6 +248,7 @@ protected Object valueOf(@SuppressWarnings("unused") Object thisObj) {
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetOffsetNanosecondsFor extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneGetOffsetNanosecondsFor(JSContext context, JSBuiltin builtin) {
Expand All @@ -231,8 +257,9 @@ protected JSTemporalTimeZoneGetOffsetNanosecondsFor(JSContext context, JSBuiltin

@Specialization
protected double getOffsetNanosecondsFor(Object thisObj, Object instantParam,
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
JSTemporalInstantObject instant = toTemporalInstantNode.execute(instantParam);
if (timeZone.getNanoseconds() != null) {
return timeZone.getNanoseconds().doubleValue();
Expand All @@ -241,6 +268,7 @@ protected double getOffsetNanosecondsFor(Object thisObj, Object instantParam,
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetOffsetStringFor extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneGetOffsetStringFor(JSContext context, JSBuiltin builtin) {
Expand All @@ -249,13 +277,15 @@ protected JSTemporalTimeZoneGetOffsetStringFor(JSContext context, JSBuiltin buil

@Specialization
protected TruffleString getOffsetStringFor(Object thisObj, Object instantParam,
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
JSDynamicObject instant = toTemporalInstantNode.execute(instantParam);
return TemporalUtil.builtinTimeZoneGetOffsetStringFor(timeZone, instant);
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetPlainDateTimeFor extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneGetPlainDateTimeFor(JSContext context, JSBuiltin builtin) {
Expand All @@ -265,14 +295,16 @@ protected JSTemporalTimeZoneGetPlainDateTimeFor(JSContext context, JSBuiltin bui
@Specialization
protected JSDynamicObject getPlainDateTimeFor(Object thisObj, Object instantParam, Object calendarLike,
@Cached("create(getContext())") ToTemporalCalendarWithISODefaultNode toTemporalCalendarWithISODefaultNode,
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
JSDynamicObject instant = toTemporalInstantNode.execute(instantParam);
JSDynamicObject calendar = toTemporalCalendarWithISODefaultNode.executeDynamicObject(calendarLike);
return TemporalUtil.builtinTimeZoneGetPlainDateTimeFor(getContext(), timeZone, instant, calendar);
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetInstantFor extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneGetInstantFor(JSContext context, JSBuiltin builtin) {
Expand All @@ -282,15 +314,17 @@ protected JSTemporalTimeZoneGetInstantFor(JSContext context, JSBuiltin builtin)
@Specialization
protected JSDynamicObject getInstantFor(Object thisObj, Object dateTimeParam, Object optionsParam,
@Cached("create(getContext())") ToTemporalDateTimeNode toTemporalDateTime,
@Cached TruffleString.EqualNode equalNode) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached TruffleString.EqualNode equalNode,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
JSTemporalPlainDateTimeObject dateTime = (JSTemporalPlainDateTimeObject) toTemporalDateTime.executeDynamicObject(dateTimeParam, Undefined.instance);
JSDynamicObject options = getOptionsObject(optionsParam);
Disambiguation disambiguation = TemporalUtil.toTemporalDisambiguation(options, getOptionNode(), equalNode);
return TemporalUtil.builtinTimeZoneGetInstantFor(getContext(), timeZone, dateTime, disambiguation);
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetPossibleInstantsFor extends JSTemporalBuiltinOperation {

protected JSTemporalTimeZoneGetPossibleInstantsFor(JSContext context, JSBuiltin builtin) {
Expand All @@ -300,8 +334,9 @@ protected JSTemporalTimeZoneGetPossibleInstantsFor(JSContext context, JSBuiltin
@TruffleBoundary
@Specialization
protected JSDynamicObject getPossibleInstantsFor(Object thisObj, Object dateTimeParam,
@Cached("create(getContext())") ToTemporalDateTimeNode toTemporalDateTime) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached("create(getContext())") ToTemporalDateTimeNode toTemporalDateTime,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
JSTemporalPlainDateTimeObject dateTime = (JSTemporalPlainDateTimeObject) toTemporalDateTime.executeDynamicObject(dateTimeParam, Undefined.instance);
JSRealm realm = getRealm();
if (timeZone.getNanoseconds() != null) {
Expand All @@ -323,6 +358,7 @@ protected JSDynamicObject getPossibleInstantsFor(Object thisObj, Object dateTime
}
}

@ImportStatic({JSConfig.class})
public abstract static class JSTemporalTimeZoneGetNextOrPreviousTransition extends JSTemporalBuiltinOperation {

private final boolean isNext;
Expand All @@ -334,8 +370,9 @@ protected JSTemporalTimeZoneGetNextOrPreviousTransition(JSContext context, JSBui

@Specialization
protected JSDynamicObject getTransition(Object thisObj, Object startingPointParam,
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj);
@Cached("create(getContext())") ToTemporalInstantNode toTemporalInstantNode,
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop) {
JSTemporalTimeZoneObject timeZone = requireTemporalTimeZone(thisObj, interop, getContext());
JSTemporalInstantObject startingPoint = toTemporalInstantNode.execute(startingPointParam);
if (timeZone.getNanoseconds() != null) {
return Null.instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ public abstract class ForeignObjectPrototypeNode extends JavaScriptBaseNode {

@Specialization(limit = "InteropLibraryLimit")
public JSDynamicObject doTruffleObject(Object truffleObject,
@CachedLibrary("truffleObject") InteropLibrary interop) {
@CachedLibrary("truffleObject") InteropLibrary interop) {
JSRealm realm = getRealm();
if (interop.hasArrayElements(truffleObject)) {
return realm.getArrayPrototype();
} else if (interop.isInstant(truffleObject)) {
return realm.getTemporalInstantPrototype();
} else if (interop.isDuration(truffleObject)) {
return realm.getTemporalDurationPrototype();
} else if (interop.isDate(truffleObject)) {
return realm.getTemporalPlainDatePrototype();
} else if (interop.isTime(truffleObject)) {
return realm.getTemporalPlainTimePrototype();
} else if (interop.isTimeZone(truffleObject)) {
return realm.getTemporalTimeZonePrototype();
} else if (interop.isExecutable(truffleObject) || interop.isInstantiable(truffleObject)) {
return realm.getFunctionPrototype();
} else if (interop.isInstant(truffleObject)) {
return realm.getDatePrototype();
} else if (interop.hasHashEntries(truffleObject)) {
return realm.getMapPrototype();
} else if (interop.hasIterator(truffleObject)) {
Expand All @@ -91,4 +99,4 @@ public static ForeignObjectPrototypeNode create() {
public static ForeignObjectPrototypeNode getUncached() {
return ForeignObjectPrototypeNodeGen.getUncached();
}
}
}