Skip to content

Commit

Permalink
Create LuaMetaType
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Jun 22, 2024
1 parent 6f39af8 commit da6fde6
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/main/java/org/glavo/lua/runtime/LuaMetaType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.glavo.lua.runtime;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import org.glavo.lua.LuaLanguage;

import java.util.function.BiPredicate;

@ExportLibrary(InteropLibrary.class)
public final class LuaMetaType implements TruffleObject {

public static final LuaMetaType NULL = new LuaMetaType("nil", InteropLibrary::isNull);
public static final LuaMetaType BOOLEAN = new LuaMetaType("boolean", InteropLibrary::isBoolean);
public static final LuaMetaType STRING = new LuaMetaType("string", InteropLibrary::isString);
public static final LuaMetaType NUMBER = new LuaMetaType("number", InteropLibrary::isNumber);
public static final LuaMetaType FUNCTION = new LuaMetaType("function", InteropLibrary::isExecutable);

private final String name;
private final BiPredicate<InteropLibrary, Object> typeCheck;

private LuaMetaType(String name, BiPredicate<InteropLibrary, Object> typeCheck) {
this.name = name;
this.typeCheck = typeCheck;
}

@ExportMessage
boolean hasLanguage() {
return true;
}

@ExportMessage
Class<? extends TruffleLanguage<?>> getLanguage() {
return LuaLanguage.class;
}

@ExportMessage
boolean isMetaObject() {
return true;
}

@ExportMessage
static class IsMetaInstance {
@Specialization(guards = "type == cachedType", limit = "3")
static boolean doCached(LuaMetaType type, Object value,
@Cached("type") LuaMetaType cachedType,
@CachedLibrary("value") InteropLibrary valueLib) {
return cachedType.typeCheck.test(valueLib, value);
}

@CompilerDirectives.TruffleBoundary
@Specialization(replaces = "doCached")
static boolean doGeneric(LuaMetaType type, Object value) {
return type.typeCheck.test(InteropLibrary.getUncached(), value);
}
}

@CompilerDirectives.TruffleBoundary
@Override
public String toString() {
return "LuaMetaType[" + name + "]";
}
}

0 comments on commit da6fde6

Please sign in to comment.