-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support method missing #672
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
605fb34
Support most basic method_missing.
st0012 49af148
Make method missing capible of receiving args correctly.
st0012 b264ce7
Format code.
st0012 d5e7158
Add more test cases.
st0012 bfd36be
Refactor instructions implementations.
st0012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,7 +207,7 @@ var builtinActions = map[operationType]*action{ | |
name: bytecode.NewArray, | ||
operation: func(t *Thread, sourceLine int, cf *normalCallFrame, args ...interface{}) { | ||
argCount := args[0].(int) | ||
elems := []Object{} | ||
var elems []Object | ||
|
||
for i := 0; i < argCount; i++ { | ||
v := t.Stack.Pop() | ||
|
@@ -228,7 +228,7 @@ var builtinActions = map[operationType]*action{ | |
t.pushErrorObject(errors.TypeError, sourceLine, "Expect stack top's value to be an Array when executing 'expandarray' instruction.") | ||
} | ||
|
||
elems := []Object{} | ||
var elems []Object | ||
|
||
for i := 0; i < arrLength; i++ { | ||
var elem Object | ||
|
@@ -279,10 +279,10 @@ var builtinActions = map[operationType]*action{ | |
name: bytecode.BranchUnless, | ||
operation: func(t *Thread, sourceLine int, cf *normalCallFrame, args ...interface{}) { | ||
v := t.Stack.Pop() | ||
bool, isBool := v.Target.(*BooleanObject) | ||
bo, isBool := v.Target.(*BooleanObject) | ||
|
||
if isBool { | ||
if bool.value { | ||
if bo.value { | ||
return | ||
} | ||
|
||
|
@@ -304,9 +304,9 @@ var builtinActions = map[operationType]*action{ | |
name: bytecode.BranchIf, | ||
operation: func(t *Thread, sourceLine int, cf *normalCallFrame, args ...interface{}) { | ||
v := t.Stack.Pop() | ||
bool, isBool := v.Target.(*BooleanObject) | ||
bo, isBool := v.Target.(*BooleanObject) | ||
|
||
if isBool && !bool.value { | ||
if isBool && !bo.value { | ||
return | ||
} | ||
|
||
|
@@ -496,7 +496,28 @@ var builtinActions = map[operationType]*action{ | |
method = receiver.findMethod(methodName) | ||
|
||
if method == nil { | ||
t.setErrorObject(receiverPr, argPr, errors.UndefinedMethodError, sourceLine, "Undefined Method '%+v' for %+v", methodName, receiver.toString()) | ||
mm := receiver.findMethodMissing() | ||
|
||
if mm == nil { | ||
t.setErrorObject(receiverPr, argPr, errors.UndefinedMethodError, sourceLine, "Undefined Method '%+v' for %+v", methodName, receiver.toString()) | ||
} else { | ||
// Move up args for missed method's name | ||
// before: | arg 1 | arg 2 | | ||
// after: | method name | arg 1 | arg 2 | | ||
// TODO: Improve this | ||
t.Stack.Push(nil) | ||
|
||
for i := argCount - 1; i >= 0; i-- { | ||
position := argPr + i | ||
arg := t.Stack.data[argPr+i] | ||
t.Stack.Set(position+1, arg) | ||
} | ||
|
||
t.Stack.Set(argPr, &Pointer{Target: t.vm.initStringObject(methodName)}) | ||
argCount++ | ||
|
||
method = mm | ||
} | ||
} | ||
|
||
// Find Block | ||
|
@@ -604,39 +625,39 @@ var builtinActions = map[operationType]*action{ | |
}, | ||
} | ||
|
||
func (vm *VM) InitObjectFromGoType(value interface{}) Object { | ||
switch v := value.(type) { | ||
func (v *VM) InitObjectFromGoType(value interface{}) Object { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method VM.InitObjectFromGoType should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
switch val := value.(type) { | ||
case nil: | ||
return NULL | ||
case int: | ||
return vm.InitIntegerObject(v) | ||
return v.InitIntegerObject(val) | ||
case int64: | ||
return vm.InitIntegerObject(int(v)) | ||
return v.InitIntegerObject(int(val)) | ||
case int32: | ||
return vm.InitIntegerObject(int(v)) | ||
return v.InitIntegerObject(int(val)) | ||
case float64: | ||
return vm.initFloatObject(v) | ||
return v.initFloatObject(val) | ||
case []uint8: | ||
bytes := []byte{} | ||
var bytes []byte | ||
|
||
for _, i := range v { | ||
for _, i := range val { | ||
bytes = append(bytes, byte(i)) | ||
} | ||
|
||
return vm.initStringObject(string(bytes)) | ||
return v.initStringObject(string(bytes)) | ||
case string: | ||
return vm.initStringObject(v) | ||
return v.initStringObject(val) | ||
case bool: | ||
return toBooleanObject(v) | ||
return toBooleanObject(val) | ||
case []interface{}: | ||
var objs []Object | ||
var objects []Object | ||
|
||
for _, elem := range v { | ||
objs = append(objs, vm.InitObjectFromGoType(elem)) | ||
for _, elem := range val { | ||
objects = append(objects, v.InitObjectFromGoType(elem)) | ||
} | ||
|
||
return vm.InitArrayObject(objs) | ||
return v.InitArrayObject(objects) | ||
default: | ||
return vm.initGoObject(value) | ||
return v.initGoObject(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported method VM.InitObjectFromGoType should have comment or be unexported