Skip to content

Commit

Permalink
Share single InteropLibrary among all resolveInterop calls
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed May 24, 2023
1 parent df0a771 commit ca1b79a
Showing 1 changed file with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package org.enso.interpreter.node.expression.builtin.meta;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateUncached;
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.nodes.Node;
import org.enso.interpreter.dsl.AcceptsError;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
Expand All @@ -22,6 +13,17 @@
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateUncached;
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.nodes.Node;

@BuiltinMethod(
type = "Meta",
name = "type_of",
Expand Down Expand Up @@ -108,18 +110,24 @@ Object doAny(Object value) {
abstract static class WithoutType extends Node {
abstract Object execute(Object value);

@Specialization(guards = {"findInterop(value, interop).isArray()"})
Type doPolyglotArray(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isArray()"})
Type doPolyglotArray(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
return EnsoContext.get(this).getBuiltins().array();
}

@Specialization(guards = {"findInterop(value, interop).isString()"})
Type doPolyglotString(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isString()"})
Type doPolyglotString(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
return EnsoContext.get(this).getBuiltins().text();
}

@Specialization(guards = {"findInterop(value, interop).isNumber()"})
Type doPolyglotNumber(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isNumber()"})
Type doPolyglotNumber(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
if (interop.fitsInInt(value)) {
return builtins.number().getInteger();
Expand All @@ -130,37 +138,49 @@ Type doPolyglotNumber(Object value, @CachedLibrary(limit = "3") InteropLibrary i
}
}

@Specialization(guards = {"findInterop(value, interop).isDateTime()"})
Type doDateTime(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isDateTime()"})
Type doDateTime(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
return EnsoContext.get(this).getBuiltins().dateTime();
}

@Specialization(guards = {"findInterop(value, interop).isTimeZone()"})
Type doTimeZone(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isTimeZone()"})
Type doTimeZone(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
EnsoContext ctx = EnsoContext.get(this);
return ctx.getBuiltins().timeZone();
}

@Specialization(guards = {"findInterop(value, interop).isDate()"})
Type doDate(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isDate()"})
Type doDate(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
EnsoContext ctx = EnsoContext.get(this);
return ctx.getBuiltins().date();
}

@Specialization(guards = {"findInterop(value, interop).isTime()"})
Type doTime(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isTime()"})
Type doTime(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
EnsoContext ctx = EnsoContext.get(this);
return ctx.getBuiltins().timeOfDay();
}

@Specialization(guards = "findInterop(value, interop).isDuration()")
Type doDuration(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = "resolveInterop(value, interop).isDuration()")
Type doDuration(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
EnsoContext ctx = EnsoContext.get(this);
return ctx.getBuiltins().duration();
}

@Specialization(guards = {"findInterop(value, interop).isMetaObject()"})
Object doMetaObject(Object value, @CachedLibrary(limit = "3") InteropLibrary interop) {
@Specialization(guards = {"resolveInterop(value, interop).isMetaObject()"})
Object doMetaObject(
Object value,
@CachedLibrary(limit = "3") @Shared("resolveInterop") InteropLibrary interop) {
try {
return interop.getMetaObject(value);
} catch (UnsupportedMessageException e) {
Expand All @@ -181,11 +201,11 @@ Object doAny(Object value) {
this);
}

static InteropType findInterop(Object value, InteropLibrary interop) {
return InteropType.find(value, interop);
static Interop resolveInterop(Object value, InteropLibrary interop) {
return Interop.resolve(value, interop);
}

enum InteropType {
enum Interop {
NONE,
STRING,
NUMBER,
Expand All @@ -197,7 +217,7 @@ enum InteropType {
DURATION,
META_OBJECT;

static InteropType find(Object value, InteropLibrary interop) {
static Interop resolve(Object value, InteropLibrary interop) {
if (interop.isString(value)) {
return STRING;
}
Expand Down

0 comments on commit ca1b79a

Please sign in to comment.