Skip to content

Commit

Permalink
Meta.get_constructor_fields works for Type
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Mar 10, 2023
1 parent 5cf58d6 commit 0a6ac0a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.enso.interpreter.node.expression.builtin.meta;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.callable.argument.ArgumentDefinition;
Expand All @@ -12,13 +14,25 @@
name = "get_constructor_fields",
description = "Gets the field names of a constructor.",
autoRegister = false)
public class GetConstructorFieldNamesNode extends Node {
Array execute(AtomConstructor atom_constructor) {
public abstract class GetConstructorFieldNamesNode extends Node {
static GetConstructorFieldNamesNode build() {
return GetConstructorFieldNamesNodeGen.create();
}

abstract Array execute(Object obj);

@Specialization
final Array executeAtomConstructor(AtomConstructor atom_constructor) {
ArgumentDefinition[] fields = atom_constructor.getFields();
Object[] result = new Object[fields.length];
for (int i = 0; i < fields.length; i++) {
result[i] = Text.create(fields[i].getName());
}
return new Array(result);
}

@Fallback
final Array executeAny(Object any) {
return Array.empty();
}
}
12 changes: 12 additions & 0 deletions test/Tests/src/Semantic/Meta_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ spec =
e_tpe . should_equal_type IOException
e_tpe . should_not_equal_type JException

Test.specify "fields of a Type" <|
typ = Boolean

Meta.is_atom typ . should_be_true
meta_typ = Meta.meta typ
meta_typ . should_be_a Meta.Atom
fields = case meta_typ of
Meta.Atom.Value _ -> meta_typ.constructor.fields
_ -> Test.fail "Should be a Meta.Atom.Value: " + meta_typ.to_text

fields . should_equal []

Test.specify "should correctly handle Java values" <|
java_meta = Meta.meta Random.new
java_meta . should_be_a Meta.Polyglot.Value
Expand Down

0 comments on commit 0a6ac0a

Please sign in to comment.