Browse code

app_lua: register the functions exported by modules for kemi

Daniel-Constantin Mierla authored on 12/04/2016 06:18:24
Showing 3 changed files
... ...
@@ -183,6 +183,7 @@ void lua_sr_openlibs(lua_State *L)
183 183
 	lua_sr_core_openlibs(L);
184 184
 	lua_sr_exp_openlibs(L);
185 185
 	lua_sr_kemi_register_core(L);
186
+	lua_sr_kemi_register_modules(L);
186 187
 }
187 188
 
188 189
 /**
... ...
@@ -1421,60 +1421,6 @@ void lua_sr_core_openlibs(lua_State *L)
1421 1421
 }
1422 1422
 
1423 1423
 
1424
-/**
1425
- *
1426
- */
1427
-static int lua_sr_kemi_dbg(sip_msg_t *msg, str *txt)
1428
-{
1429
-	if(txt!=NULL && txt->s!=NULL)
1430
-		LM_DBG("%.*s", txt->len, txt->s);
1431
-	return 0;
1432
-}
1433
-
1434
-/**
1435
- *
1436
- */
1437
-static int lua_sr_kemi_err(sip_msg_t *msg, str *txt)
1438
-{
1439
-	if(txt!=NULL && txt->s!=NULL)
1440
-		LM_ERR("%.*s", txt->len, txt->s);
1441
-	return 0;
1442
-}
1443
-
1444
-/**
1445
- *
1446
- */
1447
-static int lua_sr_kemi_info(sip_msg_t *msg, str *txt)
1448
-{
1449
-	if(txt!=NULL && txt->s!=NULL)
1450
-		LM_INFO("%.*s", txt->len, txt->s);
1451
-	return 0;
1452
-}
1453
-
1454
-
1455
-/**
1456
- *
1457
- */
1458
-static sr_kemi_t _sr_kemi_core[] = {
1459
-	{ str_init(""), str_init("dbg"),
1460
-		SR_KEMIP_NONE, lua_sr_kemi_dbg,
1461
-		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1462
-			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1463
-	},
1464
-	{ str_init(""), str_init("err"),
1465
-		SR_KEMIP_NONE, lua_sr_kemi_err,
1466
-		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1467
-			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1468
-	},
1469
-	{ str_init(""), str_init("info"),
1470
-		SR_KEMIP_NONE, lua_sr_kemi_info,
1471
-		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1472
-			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1473
-	},
1474
-
1475
-	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1476
-};
1477
-
1478 1424
 /**
1479 1425
  *
1480 1426
  */
... ...
@@ -1493,7 +1439,7 @@ int sr_kemi_return(lua_State* L, sr_kemi_t *ket, int rc)
1493 1439
 /**
1494 1440
  *
1495 1441
  */
1496
-int sr_kemi_exec_func(lua_State* L, str *mname, str *fname)
1442
+int sr_kemi_exec_func(lua_State* L, str *mname, int midx, str *fname)
1497 1443
 {
1498 1444
 	int i;
1499 1445
 	int pdelta;
... ...
@@ -1505,20 +1451,14 @@ int sr_kemi_exec_func(lua_State* L, str *mname, str *fname)
1505 1451
 
1506 1452
 	env_L = sr_lua_env_get();
1507 1453
 
1454
+	ket = sr_kemi_lookup(mname, midx, fname);
1455
+	if(ket==NULL) {
1456
+		return app_lua_return_false(L);
1457
+	}
1508 1458
 	if(mname->len<=0) {
1509 1459
 		pdelta = 1;
1510
-		for(i=0; ; i++) {
1511
-			ket = &_sr_kemi_core[i];
1512
-			if(ket->fname.len==fname->len
1513
-					&& strncasecmp(ket->fname.s, fname->s, fname->len)==0) {
1514
-				break;
1515
-			}
1516
-		}
1517 1460
 	} else {
1518
-		pdelta = 2;
1519
-	}
1520
-	if(ket==NULL) {
1521
-		return app_lua_return_false(L);
1461
+		pdelta = 3;
1522 1462
 	}
1523 1463
 
1524 1464
 	argc = lua_gettop(L);
... ...
@@ -1555,12 +1495,15 @@ int sr_kemi_exec_func(lua_State* L, str *mname, str *fname)
1555 1495
 			return app_lua_return_false(L);
1556 1496
 		}
1557 1497
 	}
1498
+
1558 1499
 	switch(i) {
1559 1500
 		case 1:
1560 1501
 			if(ket->ptypes[0]==SR_KEMIP_INT) {
1561 1502
 				ret = ((sr_kemi_fmn_f)(ket->func))(env_L->msg, vps[0].n);
1503
+				return sr_kemi_return(L, ket, ret);
1562 1504
 			} else if(ket->ptypes[0]==SR_KEMIP_STR) {
1563 1505
 				ret = ((sr_kemi_fms_f)(ket->func))(env_L->msg, &vps[0].s);
1506
+				return sr_kemi_return(L, ket, ret);
1564 1507
 			} else {
1565 1508
 				LM_ERR("invalid parameters for: %.*s\n",
1566 1509
 						fname->len, fname->s);
... ...
@@ -1571,8 +1514,10 @@ int sr_kemi_exec_func(lua_State* L, str *mname, str *fname)
1571 1514
 			if(ket->ptypes[0]==SR_KEMIP_INT) {
1572 1515
 				if(ket->ptypes[1]==SR_KEMIP_INT) {
1573 1516
 					ret = ((sr_kemi_fmnn_f)(ket->func))(env_L->msg, vps[0].n, vps[1].n);
1517
+					return sr_kemi_return(L, ket, ret);
1574 1518
 				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
1575 1519
 					ret = ((sr_kemi_fmns_f)(ket->func))(env_L->msg, vps[0].n, &vps[1].s);
1520
+					return sr_kemi_return(L, ket, ret);
1576 1521
 				} else {
1577 1522
 					LM_ERR("invalid parameters for: %.*s\n",
1578 1523
 							fname->len, fname->s);
... ...
@@ -1581,8 +1526,10 @@ int sr_kemi_exec_func(lua_State* L, str *mname, str *fname)
1581 1526
 			} else if(ket->ptypes[0]==SR_KEMIP_STR) {
1582 1527
 				if(ket->ptypes[1]==SR_KEMIP_INT) {
1583 1528
 					ret = ((sr_kemi_fmsn_f)(ket->func))(env_L->msg, &vps[0].s, vps[1].n);
1529
+					return sr_kemi_return(L, ket, ret);
1584 1530
 				} else if(ket->ptypes[1]==SR_KEMIP_STR) {
1585 1531
 					ret = ((sr_kemi_fmss_f)(ket->func))(env_L->msg, &vps[0].s, &vps[1].s);
1532
+					return sr_kemi_return(L, ket, ret);
1586 1533
 				} else {
1587 1534
 					LM_ERR("invalid parameters for: %.*s\n",
1588 1535
 							fname->len, fname->s);
... ...
@@ -1595,27 +1542,59 @@ int sr_kemi_exec_func(lua_State* L, str *mname, str *fname)
1595 1542
 			}
1596 1543
 		break;
1597 1544
 		case 3:
1598
-			LM_ERR("too many parameters for: %.*s\n",
1545
+			LM_ERR("invalid parameters for: %.*s\n",
1599 1546
 					fname->len, fname->s);
1600 1547
 			return app_lua_return_false(L);
1601 1548
 		break;
1602 1549
 		case 4:
1603
-			LM_ERR("too many parameters for: %.*s\n",
1604
-					fname->len, fname->s);
1605
-			return app_lua_return_false(L);
1550
+			if(ket->ptypes[0]==SR_KEMIP_STR
1551
+					|| ket->ptypes[1]==SR_KEMIP_STR
1552
+					|| ket->ptypes[2]==SR_KEMIP_STR
1553
+					|| ket->ptypes[3]==SR_KEMIP_STR) {
1554
+				ret = ((sr_kemi_fmssss_f)(ket->func))(env_L->msg,
1555
+						&vps[0].s, &vps[1].s, &vps[2].s, &vps[3].s);
1556
+				return sr_kemi_return(L, ket, ret);
1557
+			} else {
1558
+				LM_ERR("invalid parameters for: %.*s\n",
1559
+						fname->len, fname->s);
1560
+				return app_lua_return_false(L);
1561
+			}
1606 1562
 		break;
1607 1563
 		case 5:
1608
-			LM_ERR("too many parameters for: %.*s\n",
1609
-					fname->len, fname->s);
1610
-			return app_lua_return_false(L);
1564
+			if(ket->ptypes[0]==SR_KEMIP_STR
1565
+					|| ket->ptypes[1]==SR_KEMIP_STR
1566
+					|| ket->ptypes[2]==SR_KEMIP_STR
1567
+					|| ket->ptypes[3]==SR_KEMIP_STR
1568
+					|| ket->ptypes[4]==SR_KEMIP_STR) {
1569
+				ret = ((sr_kemi_fmsssss_f)(ket->func))(env_L->msg,
1570
+						&vps[0].s, &vps[1].s, &vps[2].s, &vps[3].s,
1571
+						&vps[4].s);
1572
+				return sr_kemi_return(L, ket, ret);
1573
+			} else {
1574
+				LM_ERR("invalid parameters for: %.*s\n",
1575
+						fname->len, fname->s);
1576
+				return app_lua_return_false(L);
1577
+			}
1611 1578
 		break;
1612 1579
 		case 6:
1613
-			LM_ERR("too many parameters for: %.*s\n",
1614
-					fname->len, fname->s);
1615
-			return app_lua_return_false(L);
1580
+			if(ket->ptypes[0]==SR_KEMIP_STR
1581
+					|| ket->ptypes[1]==SR_KEMIP_STR
1582
+					|| ket->ptypes[2]==SR_KEMIP_STR
1583
+					|| ket->ptypes[3]==SR_KEMIP_STR
1584
+					|| ket->ptypes[4]==SR_KEMIP_STR
1585
+					|| ket->ptypes[5]==SR_KEMIP_STR) {
1586
+				ret = ((sr_kemi_fmssssss_f)(ket->func))(env_L->msg,
1587
+						&vps[0].s, &vps[1].s, &vps[2].s, &vps[3].s,
1588
+						&vps[4].s, &vps[5].s);
1589
+				return sr_kemi_return(L, ket, ret);
1590
+			} else {
1591
+				LM_ERR("invalid parameters for: %.*s\n",
1592
+						fname->len, fname->s);
1593
+				return app_lua_return_false(L);
1594
+			}
1616 1595
 		break;
1617 1596
 		default:
1618
-			LM_ERR("too many parameters for: %.*s\n",
1597
+			LM_ERR("invalid parameters for: %.*s\n",
1619 1598
 					fname->len, fname->s);
1620 1599
 			return app_lua_return_false(L);
1621 1600
 	}
... ...
@@ -1637,7 +1616,7 @@ int sr_kemi_KSR_C(lua_State* L)
1637 1616
 	}
1638 1617
 	fname.len = strlen(fname.s);
1639 1618
 	LM_DBG("function execution of: %s\n", fname.s);
1640
-	return sr_kemi_exec_func(L, &mname, &fname);
1619
+	return sr_kemi_exec_func(L, &mname, 0, &fname);
1641 1620
 }
1642 1621
 
1643 1622
 /**
... ...
@@ -1646,17 +1625,20 @@ int sr_kemi_KSR_C(lua_State* L)
1646 1625
 int sr_kemi_KSR_MOD_C(lua_State* L)
1647 1626
 {
1648 1627
 	str mname;
1628
+	int midx;
1649 1629
 	str fname;
1650 1630
 	mname.s = (char*)lua_tostring(L, 1);
1651
-	fname.s = (char*)lua_tostring(L, 2);
1631
+	midx = lua_tointeger(L, 2);
1632
+	fname.s = (char*)lua_tostring(L, 3);
1652 1633
 	if(mname.s==NULL || fname.s==NULL) {
1653 1634
 		LM_ERR("null params: %p %p\n", mname.s, fname.s);
1654 1635
 		return app_lua_return_false(L);
1655 1636
 	}
1656 1637
 	mname.len = strlen(mname.s);
1657 1638
 	fname.len = strlen(fname.s);
1658
-	LM_DBG("module function execution of: %s.%s\n", mname.s, fname.s);
1659
-	return sr_kemi_exec_func(L, &mname, &fname);
1639
+	LM_DBG("module function execution of: %s.%s (%d)\n",
1640
+			mname.s, fname.s, midx);
1641
+	return sr_kemi_exec_func(L, &mname, midx, &fname);
1660 1642
 }
1661 1643
 
1662 1644
 
... ...
@@ -1681,3 +1663,49 @@ void lua_sr_kemi_register_core(lua_State *L)
1681 1663
 		);
1682 1664
 	LM_DBG("pushin lua KSR table definition returned %d\n", ret);
1683 1665
 }
1666
+
1667
+/**
1668
+ *
1669
+ */
1670
+void lua_sr_kemi_register_module(lua_State *L, str *mname, int midx)
1671
+{
1672
+	int ret;
1673
+#define LUA_SR_SBUF_SIZE	1024
1674
+	char sbuf[LUA_SR_SBUF_SIZE];
1675
+
1676
+	ret = snprintf(sbuf, LUA_SR_SBUF_SIZE-1,
1677
+			"KSR.%.*s = {}\n"
1678
+			"KSR.%.*s.__index = function (table, key)\n"
1679
+			"  return function (...)\n"
1680
+			"    return KSR_MOD_C('%.*s', %d, key, ...)\n"
1681
+			"  end\n"
1682
+			"end\n"
1683
+			"setmetatable(KSR.%.*s, KSR.%.*s)\n",
1684
+			mname->len, mname->s,
1685
+			mname->len, mname->s,
1686
+			mname->len, mname->s,
1687
+			midx,
1688
+			mname->len, mname->s,
1689
+			mname->len, mname->s
1690
+		);
1691
+	ret = luaL_dostring(L, sbuf);
1692
+
1693
+	LM_DBG("pushin lua KSR table definition returned %d\n", ret);
1694
+}
1695
+
1696
+/**
1697
+ *
1698
+ */
1699
+void lua_sr_kemi_register_modules(lua_State *L)
1700
+{
1701
+	int n;
1702
+	int i;
1703
+	sr_kemi_module_t *kmods;
1704
+
1705
+	n = sr_kemi_modules_size_get();
1706
+	kmods = sr_kemi_modules_get();
1707
+
1708
+	for(i=1; i<n; i++) {
1709
+		lua_sr_kemi_register_module(L, &kmods[i].mname, i);
1710
+	}
1711
+}
... ...
@@ -26,6 +26,7 @@
26 26
 
27 27
 void lua_sr_core_openlibs(lua_State *L);
28 28
 void lua_sr_kemi_register_core(lua_State *L);
29
+void lua_sr_kemi_register_modules(lua_State *L);
29 30
 
30 31
 #endif
31 32