@@ -35,11 +35,54 @@ struct eco_ubus_object {
35
35
36
36
static const char * obj_registry = "eco.ubus{obj}" ;
37
37
38
- static void lua_push_ubus_ctx (lua_State * L , struct eco_ubus_context * ctx )
38
+ /* ctx_env[ptr] = userdata */
39
+ static int lua_save_obj_to_ubus_ctx (lua_State * L , void * obj )
40
+ {
41
+ lua_getuservalue (L , 1 );
42
+
43
+ lua_pushlightuserdata (L , obj );
44
+ lua_pushvalue (L , -3 );
45
+ lua_rawset (L , -3 );
46
+
47
+ lua_settop (L , -2 );
48
+
49
+ return 1 ;
50
+ }
51
+
52
+ static void lua_get_obj_env_from_ubus_ctx (lua_State * L , struct eco_ubus_context * ctx , void * obj )
39
53
{
40
54
lua_rawgetp (L , LUA_REGISTRYINDEX , & obj_registry );
41
55
lua_rawgetp (L , -1 , ctx );
42
- lua_remove (L , -2 );
56
+ lua_getuservalue (L , -1 ); /* ..., reg, ctx, ctx env */
57
+
58
+ lua_pushlightuserdata (L , obj ); /* ..., reg, ctx, ctx env, ptr */
59
+ lua_rawget (L , -2 ); /* ..., reg, ctx, ctx env, handler */
60
+ lua_getuservalue (L , -1 ); /* ..., reg, ctx, ctx env, handler, handler env */
61
+
62
+ lua_replace (L , -5 ); /* ..., handler env, ctx, ctx env, handler */
63
+ lua_pop (L , 3 );
64
+ }
65
+
66
+ static void lua_get_obj_cb_from_ubus_ctx_by_idx (lua_State * L , struct eco_ubus_context * ctx ,
67
+ void * obj , int idx )
68
+ {
69
+ lua_pushnil (L );
70
+ lua_get_obj_env_from_ubus_ctx (L , ctx , obj ); /* ..., nil, handler env */
71
+ lua_rawgeti (L , -1 , idx ); /* ..., nil, handler env, func */
72
+
73
+ lua_replace (L , -3 ); /* ..., func, handler env */
74
+ lua_pop (L , 1 ); /* ..., func */
75
+ }
76
+
77
+ static void lua_get_obj_cb_from_ubus_ctx_by_str (lua_State * L , struct eco_ubus_context * ctx ,
78
+ void * obj , const char * name )
79
+ {
80
+ lua_pushnil (L );
81
+ lua_get_obj_env_from_ubus_ctx (L , ctx , obj ); /* ..., nil, handler env */
82
+ lua_getfield (L , -1 , name ); /* ..., nil, handler env, func */
83
+
84
+ lua_replace (L , -3 ); /* ..., func, handler env */
85
+ lua_pop (L , 1 ); /* ..., func */
43
86
}
44
87
45
88
static void lua_table_to_blob (lua_State * L , int index , struct blob_buf * b , bool is_array )
@@ -392,21 +435,7 @@ static void lua_ubus_event_handler(struct ubus_context *ctx, struct ubus_event_h
392
435
struct eco_ubus_context * c = container_of (ctx , struct eco_ubus_context , ctx );
393
436
lua_State * L = c -> eco -> L ;
394
437
395
- lua_pushnil (L );
396
-
397
- lua_push_ubus_ctx (L , c );
398
- lua_getuservalue (L , -1 ); /* ..., nil, ctx, ctx env */
399
-
400
- lua_pushlightuserdata (L , ev ); /* ..., nil, ctx, ctx env, ptr */
401
- lua_rawget (L , -2 ); /* ..., nil, ctx, ctx env, handler */
402
-
403
- lua_getuservalue (L , -1 ); /* ..., nil, ctx, ctx env, handler, handler env */
404
-
405
- lua_rawgeti (L , -1 , 1 ); /* ..., nil, ctx, ctx env, handler, handler env, func */
406
-
407
- lua_replace (L , -6 ); /* ..., func, ctx, ctx env, handler, handler env */
408
-
409
- lua_settop (L , -5 ); /* ..., func */
438
+ lua_get_obj_cb_from_ubus_ctx_by_idx (L , c , ev , 1 );
410
439
411
440
lua_pushstring (L , type );
412
441
@@ -441,13 +470,7 @@ static int lua_ubus_listen(lua_State *L)
441
470
return 2 ;
442
471
}
443
472
444
- lua_getuservalue (L , 1 ); /* 1: ctx 2: event name 3: func 4: ev handler 5: ctx env */
445
-
446
- lua_pushlightuserdata (L , e ); /* 1: ctx 2: event name 3: func 4: handler 5: ctx env, 6: handler ptr */
447
- lua_pushvalue (L , -3 ); /* 1: ctx 2: event name 3: func 4: handler 5: ctx env, 6: handler ptr, 7: handler */
448
- lua_rawset (L , -3 ); /* ctx_env[ptr] = handler */
449
-
450
- lua_settop (L , -2 ); /* 1: ctx 2: event name 3: func 4: handler */
473
+ lua_save_obj_to_ubus_ctx (L , e );
451
474
452
475
return 1 ;
453
476
}
@@ -468,19 +491,7 @@ static int ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj
468
491
469
492
lua_settop (L , 0 );
470
493
471
- lua_pushnil (L );
472
-
473
- lua_push_ubus_ctx (L , c );
474
- lua_getuservalue (L , -1 );
475
-
476
- lua_pushlightuserdata (L , o );
477
- lua_rawget (L , -2 );
478
-
479
- lua_getuservalue (L , -1 );
480
- lua_getfield (L , -1 , method );
481
-
482
- lua_replace (L , -6 );
483
- lua_settop (L , -5 );
494
+ lua_get_obj_cb_from_ubus_ctx_by_str (L , c , o , method );
484
495
485
496
ubus_defer_request (ctx , req , dreq );
486
497
@@ -621,15 +632,7 @@ static int lua_ubus_add(lua_State *L)
621
632
return 2 ;
622
633
}
623
634
624
- lua_getuservalue (L , 1 );
625
-
626
- lua_pushlightuserdata (L , o );
627
- lua_pushvalue (L , -3 );
628
- lua_rawset (L , -3 );
629
-
630
- lua_settop (L , -2 );
631
-
632
- return 1 ;
635
+ return lua_save_obj_to_ubus_ctx (L , o );
633
636
}
634
637
635
638
static int lua_ubus_reply (lua_State * L )
@@ -670,21 +673,7 @@ static int ubus_subscriber_cb(struct ubus_context *ctx, struct ubus_object *obj,
670
673
struct ubus_subscriber * s = container_of (obj , struct ubus_subscriber , obj );
671
674
lua_State * L = c -> eco -> L ;
672
675
673
- lua_pushnil (L );
674
-
675
- lua_push_ubus_ctx (L , c );
676
- lua_getuservalue (L , -1 );
677
-
678
- lua_pushlightuserdata (L , s );
679
- lua_rawget (L , -2 );
680
-
681
- lua_getuservalue (L , -1 );
682
-
683
- lua_rawgeti (L , -1 , 1 );
684
-
685
- lua_replace (L , -6 );
686
-
687
- lua_settop (L , -5 );
676
+ lua_get_obj_cb_from_ubus_ctx_by_idx (L , c , s , 1 );
688
677
689
678
lua_pushstring (L , method );
690
679
@@ -733,15 +722,7 @@ static int lua_ubus_subscribe(lua_State *L)
733
722
return 2 ;
734
723
}
735
724
736
- lua_getuservalue (L , 1 );
737
-
738
- lua_pushlightuserdata (L , sub );
739
- lua_pushvalue (L , -3 );
740
- lua_rawset (L , -3 );
741
-
742
- lua_settop (L , -2 );
743
-
744
- return 0 ;
725
+ return lua_save_obj_to_ubus_ctx (L , sub );
745
726
}
746
727
747
728
static int lua_ubus_notify (lua_State * L )
0 commit comments