读取txt或者字符串中的aar函数代码,并通过loadcode() 来加载执行此函数
By
admin
at 2018-08-26 • 1人收藏 • 1554人看过
如题,有个需求是能过加载并执行一个txt或者ini文件中的字符串,这个字符串中包含需要被调用的函数方法,程序里要执行此方法。
import console; var sss=/****** xieyi = function(str){ var dataStr = string.left(string.replace(str,"\s",""),4); var ret = (10-tonumber(dataStr)*0.01); return ret; } ******/ var xx = loadcode(sss) console.varDump(xx()) console.varDump(xieyi("50 01")) console.pause(true);
说一说注意关键点:
1,可以在字符串里添加
var str1,str2 = ...
来动态的获取外部提供的输入数据,但是这个数据需要在xx(str1,str2)的时候写
2,函数只需要执行一次xx()初始化,之后就可以无限制的使用xieyi()函数了。
3 个回复 | 最后更新于 2018-08-26
import console; var sss=/****** xxtab={ { geshi = "41"; xieyi = function(str){ var dataStr = string.left(string.replace(str,"\s",""),4); var ret = (10-tonumber(dataStr)*0.01); return ret; } }; { geshi = "a3 50 01"; xieyi = function(str){ var dataStr = string.left(string.replace(str,"\s",""),4); var ret = (tonumber(dataStr,16)*0.01 -100); return ret; } } } ******/ var xx = loadcode(sss) console.varDump(xx()) console.varDump(xxtab[1].geshi) console.varDump(xxtab[1].xieyi("50 01")) console.varDump(xxtab[2].geshi) console.varDump(xxtab[2].xieyi("50 01")) console.pause(true);
如果把loadcode()改为loadcodex(),那么就可以少写一个执行初始化的语句了
import console; var sss=/****** xxtab={ { geshi = "41"; xieyi = function(str){ var dataStr = string.left(string.replace(str,"\s",""),4); var ret = (10-tonumber(dataStr)*0.01); return ret; } }; { geshi = "a3 50 01"; xieyi = function(str){ var dataStr = string.left(string.replace(str,"\s",""),4); var ret = (tonumber(dataStr,16)*0.01 -100); return ret; } } } ******/ //改为loadcodex之后,就会立即初始化一次这个函数,就不用手动xx()了 var xx = loadcodex(sss) //console.varDump(xx()) console.varDump(xxtab[1].geshi) console.varDump(xxtab[1].xieyi("50 01")) console.varDump(xxtab[2].geshi) console.varDump(xxtab[2].xieyi("50 01")) console.pause(true);
登录后方可回帖
更进一步:
如果字符串中是一个多维度Tab表,而且表里有字符串和函数,那么