Skip to content

Commit

Permalink
changed callback(func,args) to func.callback(args), removed callback …
Browse files Browse the repository at this point in the history
…keyword
  • Loading branch information
Simn committed Dec 23, 2012
1 parent 87b95ad commit 6086a8f
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 63 deletions.
2 changes: 0 additions & 2 deletions ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ type keyword =
| Typedef
| Dynamic
| Package
| Callback
| Inline
| Using
| Null
Expand Down Expand Up @@ -392,7 +391,6 @@ let s_keyword = function
| Typedef -> "typedef"
| Dynamic -> "dynamic"
| Package -> "package"
| Callback -> "callback"
| Inline -> "inline"
| Using -> "using"
| Null -> "null"
Expand Down
2 changes: 1 addition & 1 deletion lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let keywords =
Break;Return;Continue;Extends;Implements;Import;
Switch;Case;Default;Public;Private;Try;Untyped;
Catch;New;This;Throw;Extern;Enum;In;Interface;
Cast;Override;Dynamic;Typedef;Package;Callback;
Cast;Override;Dynamic;Typedef;Package;
Inline;Using;Null;True;False;Abstract];
h

Expand Down
1 change: 0 additions & 1 deletion parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ and expr = parser
| [< '(Kwd True,p); s >] -> expr_next (EConst (Ident "true"),p) s
| [< '(Kwd False,p); s >] -> expr_next (EConst (Ident "false"),p) s
| [< '(Kwd Null,p); s >] -> expr_next (EConst (Ident "null"),p) s
| [< '(Kwd Callback,p); s >] -> expr_next (EConst (Ident "callback"),p) s
| [< '(Kwd Cast,p1); s >] ->
(match s with parser
| [< '(POpen,_); e = expr; s >] ->
Expand Down
8 changes: 4 additions & 4 deletions std/haxe/remoting/LocalConnection.hx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class LocalConnection implements AsyncConnection, implements Dynamic<AsyncConnec
},[]);
#if flash9
l.client = {
remotingCall : callback(remotingCall,c),
remotingResult : callback(remotingResult,c),
remotingCall : remotingCall.callback(c),
remotingResult : remotingResult.callback(c),
};
l.addEventListener(flash.events.StatusEvent.STATUS, function(s:flash.events.StatusEvent) {
if( s.level != "status" )
Expand All @@ -139,8 +139,8 @@ class LocalConnection implements AsyncConnection, implements Dynamic<AsyncConnec
for( d in allowDomains )
l.allowDomain(d);
#else
Reflect.setField(l,"remotingCall",callback(remotingCall,c));
Reflect.setField(l,"remotingResult",callback(remotingResult,c));
Reflect.setField(l,"remotingCall",remotingCall.callback(c));
Reflect.setField(l,"remotingResult",remotingResult.callback(c));
l.onStatus = function(s:Dynamic) {
if( s[untyped "level"] != "status" )
c.__data.error("Failed to send data on LocalConnection");
Expand Down
12 changes: 6 additions & 6 deletions std/neko/net/ThreadServer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ThreadServer<Client,Message> {
break;
pos += m.bytes;
len -= m.bytes;
work(callback(clientMessage,c.client,m.msg));
work(clientMessage.callback(c.client,m.msg));
}
if( pos > 0 )
c.buf.blit(0,c.buf,pos,len);
Expand All @@ -115,7 +115,7 @@ class ThreadServer<Client,Message> {
t.socks.remove(s);
if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
logError(e);
work(callback(doClientDisconnected,s,infos.client));
work(doClientDisconnected.callback(s,infos.client));
}
}
while( true ) {
Expand All @@ -126,7 +126,7 @@ class ThreadServer<Client,Message> {
t.socks.push(m.s);
else if( t.socks.remove(m.s) ) {
var infos : ClientInfos<Client> = m.s.custom;
work(callback(doClientDisconnected,m.s,infos.client));
work(doClientDisconnected.callback(m.s,infos.client));
}
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ class ThreadServer<Client,Message> {
if( neko.vm.Thread.current() == worker )
onError(e,stack);
else
work(callback(onError,e,stack));
work(onError.callback(e,stack));
}

function addClient( sock : sys.net.Socket ) {
Expand Down Expand Up @@ -208,13 +208,13 @@ class ThreadServer<Client,Message> {
p : new neko.net.Poll(maxSockPerThread),
};
threads.push(t);
t.t = neko.vm.Thread.create(callback(runThread,t));
t.t = neko.vm.Thread.create(runThread.callback(t));
}
}

public function addSocket( s : sys.net.Socket ) {
s.setBlocking(false);
work(callback(addClient,s));
work(addClient.callback(s));
}

public function run( host, port ) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Test.hx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ package unit;

function async<Args,T>( f : Args -> (T -> Void) -> Void, args : Args, v : T, ?pos : haxe.PosInfos ) {
if( asyncWaits.length >= AMAX ) {
asyncCache.push(callback(async,f,args,v,pos));
asyncCache.push(async.callback(f,args,v,pos));
return;
}
asyncWaits.push(pos);
Expand All @@ -101,7 +101,7 @@ package unit;

function asyncExc<Args>( seterror : (Dynamic -> Void) -> Void, f : Args -> (Dynamic -> Void) -> Void, args : Args, ?pos : haxe.PosInfos ) {
if( asyncWaits.length >= AMAX ) {
asyncCache.push(callback(asyncExc,seterror,f,args,pos));
asyncCache.push(asyncExc.callback(seterror,f,args,pos));
return;
}
asyncWaits.push(pos);
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/TestMisc.hx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class TestMisc extends Test {
var c = new MyClass(100);
var add = c.add;
eq( c.add(1,2), 103 );
eq( callback(c.add,1)(2), 103 );
eq( c.add.callback(1)(2), 103 );
eq( add(1,2), 103 );

var x = 4;
Expand Down Expand Up @@ -177,8 +177,8 @@ class TestMisc extends Test {

function testCaptureUnique2() {
// another more specialized test (was actually the original broken code - but not reproducible when optimization is off)
var foo = callback(id, 3);
var bar = callback(sq, 5);
var foo = id.callback(3);
var bar = sq.callback(5);
eq( foo(), 3 );
eq( bar(), 25 );
}
Expand Down Expand Up @@ -249,35 +249,35 @@ class TestMisc extends Test {
var inst = new MyDynamicClass(100);
var add = inst.add;
eq( inst.add(1,2), 103 );
eq( callback(inst.add,1)(2), 103 );
eq( inst.add.callback(1)(2), 103 );
eq( add(1,2), 103 );

// check overriden dynamic method
var inst = new MyDynamicSubClass(100);
var add = inst.add;
eq( inst.add(1,2), 206 );
eq( callback(inst.add,1)(2), 206 );
eq( inst.add.callback(1)(2), 206 );
eq( add(1,2), 206 );

// check overriden dynamic method
var inst = new MyDynamicSubClass2(100);
var add = inst.add;
eq( inst.add(1,2), 206 );
eq( callback(inst.add,1)(2), 206 );
eq( inst.add.callback(1)(2), 206 );
eq( add(1,2), 206 );

// check redefined dynamic method
inst.add = function(x,y) return inst.get() * 2 + x + y;
var add = inst.add;
eq( inst.add(1,2), 203 );
eq( callback(inst.add,1)(2), 203 );
eq( inst.add.callback(1)(2), 203 );
eq( add(1,2), 203 );

// check inherited dynamic method
var inst = new MyOtherDynamicClass(0);
var add = inst.add;
eq( inst.add(1,2), 13 );
eq( callback(inst.add,1)(2), 13 );
eq( inst.add.callback(1)(2), 13 );
eq( add(1,2), 13 );

// check static dynamic
Expand Down
70 changes: 35 additions & 35 deletions tests/unit/TestType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -222,81 +222,81 @@ class TestType extends Test {

// all missing

typedAs(callback(func), func);
typedAs(callback(func, _), func);
typedAs(callback(func, _, _), func);
typedAs(callback(func, _, _, _), func);
typedAs(func.callback(), func);
typedAs(func.callback(_), func);
typedAs(func.callback(_, _), func);
typedAs(func.callback(_, _, _), func);

// all given

typedAs(callback(func, 22, "2", 13), tvoid);
typedAs(func.callback(22, "2", 13), tvoid);

// last missing

typedAs(callback(func, 22, "2"), tfloat);
typedAs(callback(func, 22, "2", _), tfloat);
typedAs(func.callback(22, "2"), tfloat);
typedAs(func.callback(22, "2", _), tfloat);

// first given

typedAs(callback(func, 22), tstringfloat);
typedAs(callback(func, 22, _), tstringfloat);
typedAs(callback(func, 22, _, _), tstringfloat);
typedAs(func.callback(22), tstringfloat);
typedAs(func.callback(22, _), tstringfloat);
typedAs(func.callback(22, _, _), tstringfloat);

// mixed

typedAs(callback(func, _, _, 12), tintstring);
typedAs(callback(func, _, "22", _), tintfloat);
typedAs(callback(func, _, "22", 12), tint);
typedAs(callback(func, 12, _, 12), tstring);
typedAs(func.callback(_, _, 12), tintstring);
typedAs(func.callback(_, "22", _), tintfloat);
typedAs(func.callback(_, "22", 12), tint);
typedAs(func.callback(12, _, 12), tstring);

// values

eq(1, callback(func)(1, "2", 3));
eq(2, callback(func, 2)("2", 3));
eq(2, callback(func, 2, "3")(3));
eq(2, callback(func, 2, "3", 4)());
eq(1, func.callback()(1, "2", 3));
eq(2, func.callback(2)("2", 3));
eq(2, func.callback(2, "3")(3));
eq(2, func.callback(2, "3", 4)());

eq(1, callback(func, _, "2", 3)(1));
eq(1, callback(func, _, "2")(1, 3));
eq(1, callback(func, _)(1, "2", 3));
eq(1, func.callback(_, "2", 3)(1));
eq(1, func.callback(_, "2")(1, 3));
eq(1, func.callback(_)(1, "2", 3));

eq(1, callback(func, _, "2", _)(1, 2));
eq(1, func.callback(_, "2", _)(1, 2));

eq(1, callback(callback(func), _, "2", 3)(1));
eq(1, callback(callback(func, 1), "2", 3)());
eq(1, callback(callback(func, 1, _), "2")(3));
eq(1, callback(callback(func, _, "2"), 1)(3));
eq(1, func.callback().callback(_, "2", 3)(1));
eq(1, func.callback(1).callback("2", 3)());
eq(1, func.callback(1, _).callback("2")(3));
eq(1, func.callback(_, "2").callback(1)(3));

var a = 5;
var b = "foo";
var cb = callback(func, a);
var cb = func.callback(a);
a = 6;
func = function(a,b,c):Int return throw "error";
eq(5, cb(b, 0));

var optfunc = function(a:Int, b:Int, ?c:Int = 2) return a + b + c;
eq(6, callback(optfunc, 1)(3));
eq(6, callback(optfunc, 1, 3)());
eq(6, optfunc.callback(1)(3));
eq(6, optfunc.callback(1, 3)());

eq(7, callback(optfunc, _, _, _)(1, 2, 4));
eq(7, callback(optfunc, _, 2, _)(1, 4));
eq(7, optfunc.callback(_, _, _)(1, 2, 4));
eq(7, optfunc.callback(_, 2, _)(1, 4));

var foo = function ( x : Int, ?p : haxe.PosInfos ) { return "foo" + x; }
var f : Void -> String = callback(foo, 0);
var f : Void -> String = foo.callback(0);
eq("foo0", f());

// TODO: this fails on flash 9
var foo = function(bar = 2) { return bar; };
#if (flash9)
t(typeError(callback(foo, _)));
t(typeError(foo.callback(_)));
#else
var l = callback(foo, _);
var l = foo.callback(_);
eq(2, l());
#end

// note that this does not
var foo = function(bar:Null<Int> = 2) { return bar; };
var l = callback(foo, _);
var l = foo.callback(_);
eq(2, l());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/TestXML.hx
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ class TestXML extends Test {
h.nodeName = "em";
eq( h.nodeName, "em" );

eq( Lambda.count({ iterator : callback(x.elementsNamed,"em") }), 1 );
eq( Lambda.count({ iterator : x.elementsNamed.callback("em") }), 1 );

h.nodeName = "xhtml:em";

eq( Lambda.count({ iterator : callback(x.elementsNamed,"xhtml:em") }), 1 );
eq( Lambda.count({ iterator : callback(x.elementsNamed,"em") }), 0 );
eq( Lambda.count({ iterator : x.elementsNamed.callback("xhtml:em") }), 1 );
eq( Lambda.count({ iterator : x.elementsNamed.callback("em") }), 0 );

eq( h.nodeName, "xhtml:em" );
}
Expand Down
17 changes: 16 additions & 1 deletion typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2407,8 +2407,16 @@ and type_expr ctx ?(need_val=true) (e,p) =
mk (TCast (type_expr ctx e,Some texpr)) t p
| EDisplay (e,iscall) ->
let old = ctx.in_display in
let opt_args args ret = TFun(List.map(fun (n,o,t) -> n,true,t) args,ret) in
ctx.in_display <- true;
let e = (try type_expr ctx e with Error (Unknown_ident n,_) -> raise (Parser.TypePath ([n],None))) in
let e = match e.eexpr with
| TField (e,"callback") ->
(match follow e.etype with
| TFun(args,ret) -> {e with etype = opt_args args ret}
| _ -> e)
| _ -> e
in
ctx.in_display <- old;
let opt_type t =
match t with
Expand Down Expand Up @@ -2449,6 +2457,10 @@ and type_expr ctx ?(need_val=true) (e,p) =
PMap.fold (fun f acc -> if can_access ctx c f true then PMap.add f.cf_name { f with cf_public = true; cf_type = opt_type f.cf_type } acc else acc) a.a_fields PMap.empty
| _ ->
a.a_fields)
| TFun (args,ret) ->
let t = opt_args args ret in
let cf = mk_field "callback" (tfun [t] t) p in
PMap.add "callback" cf PMap.empty
| _ ->
PMap.empty
in
Expand Down Expand Up @@ -2526,7 +2538,10 @@ and type_call ctx e el twith p =
mk (TCall (mk (TLocal (alloc_var "`trace" t_dynamic)) t_dynamic p,[e;infos])) ctx.t.tvoid p
else
type_expr ctx (ECall ((EField ((EField ((EConst (Ident "haxe"),p),"Log"),p),"trace"),p),[e;EUntyped infos,p]),p)
| (EConst (Ident "callback"),p) , e :: params ->
| (EConst(Ident "callback"),p1),args ->
let ecb = try type_ident_raise ctx "callback" p1 MCall with Not_found -> error "callback syntax has changed to func.callback(args)" p in
build_call ctx ecb args twith p
| (EField (e,"callback"),p), params ->
type_callback ctx e params p
| (EConst (Ident "$type"),_) , [e] ->
let e = type_expr ctx e in
Expand Down

0 comments on commit 6086a8f

Please sign in to comment.