Wrap up method_missing docs and tests

This commit is contained in:
Jason Turner 2015-04-23 15:03:08 -06:00
parent 606c1d9d00
commit 9ab0b1108a
4 changed files with 61 additions and 10 deletions

View File

@ -256,6 +256,43 @@ print(m.get_value()); // prints "Value Is: a"
print(get_value(m)); // prints "Value Is: a"
```
## Dynamic Objects
All ChaiScript defined types and generic Dynamic_Object support dynamic parameters
```
var o = Dynamic_Object();
o.f = fun(x) { print(x); }
o.f(3); // prints "3"
```
Implicit 'this' is allowed:
```
var o = Dynamic_Object();
o.x = 3;
o.f = fun(y) { print(this.x + y); }
o.f(10); // prints 13
```
## method_missing
A function of the signature `method_missing(object, name, param1, param2, param3)` will be called if an appropriate
method cannot be found
```
def method_missing(int i, string name, Vector v) {
print("method_missing(${i}, ${name}), ${v.size()} params");
}
5.bob(1,2,3); // prints "method_missing(5, bob, 3 params)"
```
`method_missing` signature can be either 2 parameters or 3 parameters. If the signature contains two parameters
it is treated as a property. If the property contains a function then additional parameters are passed to
the contained function.
If both a 2 parameter and a 3 parameter signature match, the 3 parameter function always wins.
# Built In Functions

View File

@ -844,7 +844,20 @@ namespace chaiscript
// If we get here we know that either there was no method with that name,
// or there was no matching method
const auto functions = get_function("method_missing");
const auto functions = [&]()->std::vector<Proxy_Function> {
std::vector<Proxy_Function> fs;
for (const auto &f : get_function("method_missing"))
{
if(f->compare_first_type(params[0], m_conversions)) {
fs.push_back(f);
}
}
return fs;
}();
const bool is_no_param = [&]()->bool{
for (const auto &f : functions) {
@ -856,12 +869,12 @@ namespace chaiscript
}();
if (!functions.empty()) {
std::vector<Boxed_Value> tmp_params(params);
tmp_params.insert(tmp_params.begin() + 1, var(t_name));
if (is_no_param) {
std::vector<Boxed_Value> tmp_params(params);
tmp_params.insert(tmp_params.begin() + 1, var(t_name));
return do_attribute_call(2, tmp_params, functions, m_conversions);
} else {
return dispatch::dispatch(functions, tmp_params, m_conversions);
return dispatch::dispatch(functions, {params[0], var(t_name), var(std::vector<Boxed_Value>(params.begin()+1, params.end()))}, m_conversions);
}
}

View File

@ -22,12 +22,6 @@ assert_true(o.f3(4) == 60);
assert_true(o.mult(3.0) == 45.0);
def method_missing(int i, string method_name, x, y) {
"method_missing called : " + to_string(i) + "." + method_name + "(" + to_string(x) + ", " + to_string(y) + ")";
}
assert_true(5.bob(3,4) == "method_missing called : 5.bob(3, 4)" )
var o2 = Dynamic_Object();
o2.a = 15

View File

@ -0,0 +1,7 @@
def method_missing(int i, string method_name, Vector params) {
"method_missing called : " + to_string(i) + "." + method_name + "(" + to_string(params[0]) + ", " + to_string(params[1]) + ")";
}
assert_true(5.bob(3,4) == "method_missing called : 5.bob(3, 4)" )