Skip to content

Commit

Permalink
Fix uqbar-project/wollok-language#27 - mirror methods moved to object
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed May 2, 2020
1 parent 1a983fa commit 7f546f6
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 67 deletions.
2 changes: 1 addition & 1 deletion org.uqbar.project.wollok.lib/src/wollok/lang/WObject.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class WObject {
instVarName.checkNotNull("resolve")
obj.resolve(instVarName)
}

def newInstance(String className, Object... arguments) {
val wArgs = arguments.map[javaToWollok]
(interpreter.evaluator as WollokInterpreterEvaluator).newInstance(className, wArgs)
Expand Down
38 changes: 38 additions & 0 deletions org.uqbar.project.wollok.lib/src/wollok/mirror/WObjectMirror.xtend
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package wollok.mirror

import org.eclipse.xtend.lib.annotations.Accessors
import org.uqbar.project.wollok.interpreter.WollokInterpreter
import org.uqbar.project.wollok.interpreter.core.WollokObject
import wollok.lang.WObject

@Accessors(PUBLIC_GETTER)
class WObjectMirror {

WollokObject obj
WollokInterpreter interpreter

new(WollokObject obj, WollokInterpreter interpreter) {
this.obj = obj
this.interpreter = interpreter
}

def WollokObject resolve(String attributeName) {
internalTarget.resolve(attributeName)
}

def instanceVariables() {
internalTarget.instanceVariables
}

def instanceVariableFor(String name) {
internalTarget.instanceVariableFor(name)
}

def target() {
obj.resolve("target") as WollokObject
}

def internalTarget() {
new WObject(target, interpreter)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.uqbar.project.wollok.tests.sdk

import org.junit.Test
import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase

class MirrorTest extends AbstractWollokInterpreterTestCase {

@Test
def void instanceVariableFor() {
'''
import wollok.mirror.ObjectMirror
class Perro {
var nombre = "Colita"
var edad = 7
}
program p {
const perro = new Perro()
const mirror = new ObjectMirror(target = perro)
const instVar = mirror.instanceVariableFor("nombre")
assert.that(instVar != null)
assert.equals("nombre", instVar.name())
assert.equals("Colita", instVar.value())
}
'''.interpretPropagatingErrors
}

@Test
def void instanceVariables() {
'''
import wollok.mirror.ObjectMirror
class Perro {
var nombre = "Colita"
var edad = 7
}
program p {
const perro = new Perro()
const mirror = new ObjectMirror(target = perro)
const instVars = mirror.instanceVariables()
assert.equals(2, instVars.size())
const nombreInstVar = instVars.find{e=> e.name() == "nombre"}
assert.equals("Colita", nombreInstVar.value())
const edadInstVar = instVars.find{e=> e.name() == "edad"}
assert.equals(7, edadInstVar.value())
««« assert.equals("#[edad=7, nombre=Colita]",instVars.toString())
}
'''.interpretPropagatingErrors
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,50 +53,4 @@ class ObjectTest extends AbstractWollokInterpreterTestCase {
'''.interpretPropagatingErrors
}

@Test
def void instanceVariableFor() {
'''
class Perro {
var nombre = "Colita"
var edad = 7
}
program p {
const perro = new Perro()
const instVar = perro.instanceVariableFor("nombre")
assert.that(instVar != null)
assert.equals("nombre", instVar.name())
assert.equals("Colita", instVar.value())
}
'''.interpretPropagatingErrors
}

@Test
def void instanceVariables() {
'''
class Perro {
var nombre = "Colita"
var edad = 7
}
program p {
const perro = new Perro()
const instVars = perro.instanceVariables()
assert.equals(2, instVars.size())
const nombreInstVar = instVars.find{e=> e.name() == "nombre"}
assert.equals("Colita", nombreInstVar.value())
const edadInstVar = instVars.find{e=> e.name() == "edad"}
assert.equals(7, edadInstVar.value())
««« assert.equals("#[edad=7, nombre=Colita]",instVars.toString())
}
'''.interpretPropagatingErrors
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ object instanceVariableMirrorTest {
method valueToString(mirror) = mirror.valueToString()
}

object objectMirrorTest {
// XPECT methodType at resolve --> (ObjectMirror, String) => Any
method resolve(o, name) = o.resolve(name)

// XPECT methodType at instanceVariableFor --> (ObjectMirror, String) => InstanceVariableMirror
method instanceVariableFor(o, name) = o.instanceVariableFor(name)

// XPECT methodType at instanceVariables --> (ObjectMirror) => List<InstanceVariableMirror>
method instanceVariables(o) = o.instanceVariables()

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,4 @@ object objectTests {
// XPECT methodType at generateDoesNotUnderstandMessage --> (Object, String, String, Number) => String
method generateDoesNotUnderstandMessage(o, target, name, parametersCount) = o.generateDoesNotUnderstandMessage(target, name, parametersCount)


// XPECT methodType at instanceVariables --> (Object) => List<InstanceVariableMirror>
method instanceVariables(o) = o.instanceVariables()

// XPECT methodType at instanceVariableFor --> (Object, String) => InstanceVariableMirror
method instanceVariableFor(o, name) = o.instanceVariableFor(name)

// XPECT methodType at resolve --> (Object, String) => Any
method resolve(o, name) = o.resolve(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ abstract class TypeDeclarations {

def InstanceVariableMirror() { classTypeAnnotation(INSTANCE_VARIABLE_MIRROR) }

def ObjectMirror() { classTypeAnnotation(OBJECT_MIRROR) }

def StringPrinter() { classTypeAnnotation(STRING_PRINTER) }

def console() { objectTypeAnnotation(CONSOLE) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class WollokCoreTypeDeclarations extends TypeDeclarations {
/* privates */
O >> "messageNotUnderstood" === #[String, List.of(Object)] => Void
O >> "generateDoesNotUnderstandMessage" === #[String, String, Number] => String
/* introspection */
O >> "instanceVariables" === #[] => List.of(InstanceVariableMirror)
O >> "instanceVariableFor" === #[String] => InstanceVariableMirror
O >> "resolve" === #[String] => T //TODO: should return variable type
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class WollokLibTypeDeclarations extends TypeDeclarations {
InstanceVariableMirror >> "value" === #[] => T //TODO: should return variable type
InstanceVariableMirror >> "valueToString" === #[] => String

ObjectMirror >> "resolve" === #[String] => T
ObjectMirror >> "instanceVariableFor" === #[String] => InstanceVariableMirror
ObjectMirror >> "instanceVariables" === #[] => List.of(InstanceVariableMirror)

StringPrinter >> "println" === #[T] => Void
StringPrinter >> "getBuffer" === #[] => String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper

import static extension org.uqbar.project.wollok.errorHandling.WollokExceptionExtensions.*
import static org.uqbar.project.wollok.sdk.WollokSDK.*
import static org.uqbar.project.wollok.WollokConstants.*

/**
* Holds common extensions for Wollok to Java and Java to Wollok conversions.
Expand Down Expand Up @@ -78,7 +79,7 @@ class WollokJavaConversions {
primitive)
return o

throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_INVALID_CONVERSION, (o as WollokObject).call("printString"), conversionTypes.get(t) ?: t.simpleName))
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_INVALID_CONVERSION, (o as WollokObject).call(TO_STRING_PRINTABLE), conversionTypes.get(t) ?: t.simpleName))
}

def static dispatch isNativeType(Object o, String type) { false }
Expand Down Expand Up @@ -112,11 +113,11 @@ class WollokJavaConversions {
def static dispatch WollokObject convertJavaToWollok(Set<?> o) { evaluator.newInstanceWithWrapped(SET, o) }

def static dispatch WollokObject convertJavaToWollok(LocalDate o) { evaluator.newInstanceWithWrapped(DATE, o) }

def static dispatch WollokObject convertJavaToWollok(WollokObject it) { it }

def static dispatch WollokObject convertJavaToWollok(Object o) {
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_UNSUPPORTED_CONVERSION_JAVA_WOLLOK, (o as WollokObject).call("printString"), o.class.name))
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_UNSUPPORTED_CONVERSION_JAVA_WOLLOK, (o as WollokObject).call(TO_STRING_PRINTABLE), o.class.name))
}

/**
Expand Down Expand Up @@ -146,9 +147,9 @@ class WollokJavaConversions {
try {
result = o.asNumber.coerceToInteger
} catch (NumberFormatException e) {
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_INVALID_CONVERSION, o.call("printString"), "Number"))
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_INVALID_CONVERSION, o.call(TO_STRING_PRINTABLE), "Number"))
} catch (ClassCastException c) {
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_INVALID_CONVERSION, o.call("printString"), "Number"))
throw throwInvalidOperation(NLS.bind(Messages.WollokConversion_INVALID_CONVERSION, o.call(TO_STRING_PRINTABLE), "Number"))
}
result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class DefaultNativeObjectFactory implements NativeObjectFactory {
STRING -> "wollok.lang.WString",
BOOLEAN -> "wollok.lang.WBoolean",
DATE -> "wollok.lang.WDate",
SOUND -> "wollok.game.WSound"
SOUND -> "wollok.game.WSound",
OBJECT_MIRROR -> "wollok.mirror.WObjectMirror"
}

override createNativeObject(WClass it, WollokObject obj, WollokInterpreter interpreter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class WollokSDK {

public static val POSITION = "wollok.game.Position"
public static val SOUND = "wollok.game.Sound"
public static val OBJECT_MIRROR = "wollok.mirror.ObjectMirror"
public static val ASSERTION_EXCEPTION_FQN = "wollok.lib.AssertionException"
public static val STRING_PRINTER = "wollok.lib.StringPrinter"

Expand Down
2 changes: 1 addition & 1 deletion wollok-language

0 comments on commit 7f546f6

Please sign in to comment.