Skip to content

Commit

Permalink
Extend PolyglotPlugin
Browse files Browse the repository at this point in the history
- Let PrimGetMembersNode return an ArrayObject
- Introduce PrimGetMemberSizeNode, PrimIsMemberInvocableNode, and PrimIsMemberReadableNode
  • Loading branch information
fniephaus committed Jun 24, 2019
1 parent 6592cb7 commit 9145347
Showing 1 changed file with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,39 @@ protected PrimGetMembersNode(final CompiledMethodObject method) {
super(method);
}

@Specialization(guards = {"lib.hasMembers(receiver)"}, limit = "2")
protected final ArrayObject doGetMembers(final Object receiver,
@CachedLibrary("receiver") final InteropLibrary lib,
@CachedLibrary(limit = "2") final InteropLibrary membersLib,
@CachedLibrary(limit = "2") final InteropLibrary memberNameLib) {
try {
final Object members = lib.getMembers(receiver, true);
final int size = (int) membersLib.getArraySize(members);
final NativeObject[] byteStrings = new NativeObject[size];
for (int i = 0; i < size; i++) {
final Object memberName = membersLib.readArrayElement(members, i);
byteStrings[i] = method.image.asByteString(memberNameLib.asString(memberName));
}
return method.image.asArrayOfNativeObjects(byteStrings);
} catch (final UnsupportedMessageException | InvalidArrayIndexException e) {
throw SqueakException.illegalState(e);
}
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveGetMemberSize")
protected abstract static class PrimGetMemberSizeNode extends AbstractPrimitiveNode implements UnaryPrimitive {
protected PrimGetMemberSizeNode(final CompiledMethodObject method) {
super(method);
}

@Specialization(guards = {"lib.hasMembers(receiver)"}, limit = "2")
protected static final Object doGetMembers(final Object receiver,
@CachedLibrary("receiver") final InteropLibrary lib) {
@CachedLibrary("receiver") final InteropLibrary lib,
@CachedLibrary(limit = "2") final InteropLibrary sizeLib) {
try {
return lib.getMembers(receiver, true);
return sizeLib.getArraySize(lib.getMembers(receiver, true));
} catch (final UnsupportedMessageException e) {
throw SqueakException.illegalState(e);
}
Expand Down Expand Up @@ -457,6 +485,20 @@ protected final ArrayObject doList(@SuppressWarnings("unused") final Object rece
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveIsMemberInvocable")
protected abstract static class PrimIsMemberInvocableNode extends AbstractPrimitiveNode implements BinaryPrimitive {
protected PrimIsMemberInvocableNode(final CompiledMethodObject method) {
super(method);
}

@Specialization(guards = {"member.isByteType()"})
protected static final boolean doIsMemberInvocable(final Object receiver, final NativeObject member,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
return BooleanObject.wrap(lib.isMemberInvocable(receiver, member.asStringUnsafe()));
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveInvoke")
protected abstract static class PrimInvokeNode extends AbstractPrimitiveNode implements TernaryPrimitive {
Expand Down Expand Up @@ -741,6 +783,20 @@ protected static final boolean isArrayElementReadable(final InteropLibrary lib,
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveIsMemberReadable")
protected abstract static class PrimIsMemberReadableNode extends AbstractPrimitiveNode implements BinaryPrimitive {
protected PrimIsMemberReadableNode(final CompiledMethodObject method) {
super(method);
}

@Specialization(guards = {"member.isByteType()"})
protected static final boolean doIsMemberReadable(final Object receiver, final NativeObject member,
@CachedLibrary(limit = "2") final InteropLibrary lib) {
return BooleanObject.wrap(lib.isMemberReadable(receiver, member.asStringUnsafe()));
}
}

@GenerateNodeFactory
@SqueakPrimitive(names = "primitiveReadMember")
protected abstract static class PrimReadMemberNode extends AbstractPrimitiveNode implements BinaryPrimitive {
Expand Down

0 comments on commit 9145347

Please sign in to comment.