[ Content | Sidebar ]

Posts tagged javascript

Hello lua

lua将作为下一门新语言学习。如果按照一年学习一门新语言的标准来说,我算超勤快的了。lua被发明的目的便是嵌入C或C++程序,给程序带来编译语言不及的灵活性。
google了一下lua,发现大部分都是做游戏开发的C++程序员的文章,看来不只是魔兽世界在用。

lua做嵌入的优势:
1 . 小,整个解释器不到200K
实际测试:
编译一个空的IOS项目 205K
嵌入Lua后的IOS项目 414K
2 . 运行速度快
实际测试:
做了三个测试程序,分别内嵌lua(静态链接),javascript(动态链接JavascriptCore,链接库6.4M),python(动态链接python2.6,链接库2.0M)
三个程序都做相同的事情,初始化一个脚本运行环境,打印一个字符串,销毁
比如lua,其他JS和python类似,只不过调用的api不一样:

static int lua_printf(lua_State *L)
{
    const char *cmsg = luaL_checkstring(L, 1);
    printf("%s\n", cmsg);
    return 0;
}
 
static void eval_lua(NSString *code)
{
    lua_State *L;
    L = lua_open();
    luaopen_base(L);
    lua_register(L, "printf", lua_printf);
    luaL_loadstring(L, [code cStringUsingEncoding:NSUTF8StringEncoding]);
    lua_pcall(L, 0, LUA_MULTRET, 0);
    lua_close(L);
}
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
    NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
    eval_lua(@"printf(\"你好 lua\")");
    NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - start;
    printf("total time:%.5f\n", duration);
 
    [pool release];
    return 0;
}
Copy Code 

PK结果

$ ls -lh *_test
-rwxr-xr-x  1 hualu  staff    10K  6 23 20:32 js_test
-rwxr-xr-x  1 hualu  staff   136K  6 23 20:37 lua_test
-rwxr-xr-x  1 hualu  staff   9.4K  6 23 20:32 py_test
$ ./lua_test
你好 lua
total time:0.00018
$ ./js_test
你好 Javascript
total time:0.00557
$ ./py_test
你好 Python
total time:0.01311
Copy Code 

3. lua支持多线程,每个线程可以配置独立的解释器(没有亲测,道听途说)
4. 语法简单,其实这个可以算优点,比JS要简单易懂的多。。。

lua这么小巧的身躯太适合嵌入手机软件了。可以动态的从server上加载一些lua脚本来运行,免去劳烦用户更新软件的烦恼~.就目前所知,愤怒的小鸟是一个混合编程的好例子,关卡的设置均由lua控制。

其实已经有geek为前面三门语言做了Objc的Bridge,项目分别是:
对于lua有wax
对于js有jscocoa
对于python有PyObjc

而且他们的目的已经不是简单的嵌入Objectivc了,而是代替objc作为MAC或IOS应用的开发语言。。。当然我并不是很赞同这种偷懒的方法,脚本要适度使用。

最后,看的一些资料:
Lua 5.1 参考手册
xcode中添加静态链接库
lua和python谁更适用于嵌入MMORPG?

关于js的字符串编码

The String type is the set of all finite ordered sequences of zero or more 16-bit unsigned integer values (“elements”). The String type is generally used to represent textual data in a running ECMAScript program, in which case each element in the String is treated as a code unit value (see Clause 6). Each element is regarded as occupying a position within the sequence. These positions are indexed with nonnegative integers. The first element (if any) is at position 0, the next element (if any) at position 1, and so on. The length of a String is the number of elements (i.e., 16-bit values) within it. The empty String has length zero and therefore contains no elements.
When a String contains actual textual data, each element is considered to be a single UTF-16 code unit. Whether or not this is the actual storage format of a String, the characters within a String are numbered by their initial code unit element position as though they were represented using UTF-16. All operations on Strings (except as otherwise stated) treat them as sequences of undifferentiated 16-bit unsigned integers; they do not ensure the resulting String is in normalised form, nor do they ensure language-sensitive results.

按照ECMA标准,无论引擎底层如何实现,js的字符串看起来都应该是UTF-16编码的字符串,并且每个字符串单元代表一个UTF-16的双字节
例如:
>”中”.length
1
(不知道这是啥特殊字符,发到wordpress有问题,直接贴图,那我是怎么输进去的呢,后面说转义符)
“中”编码成UTF-16为一个双字节0x4E2D,所以长度为1
长的像口的字符编码成UTF-16占4字节 0xD950 0xDF21,占用两个双字节,长度为2
(下面用”口”代替这个特殊字符)
>”口”[0]
“”
>”口”.charCodeAt(0).toString(16)
“d950″
所以字符串的长度显然是占用双字节的个数,而非我以前想当然认为的实际字符的个数。。。

\udddd形式的转义同样用来表示一个双字节,而非字符本身,”口”用转义符来表示的话:

最后我要的结论是:

对于像objective c里unichar一样的UTF-16编码格式的字符串可以通过@“\\u04x”直接得到json转义串~

PPJ回忆录

个人以为:学一门脚本很容易,但是学几门就是一个痛苦的事,因为我经常把他们搞混。。

于是我下决心写一个能够帮我理清楚的文档,我命之为《PPJ回忆录》,从周5写到今天,精疲力尽,剩下OO和正则部分没写,我决定先歇会~

毫不客气的说,对于同时学里面任意两门语言的同学,这个文档都会对你有帮助。

———–
post by gmail~

一段用于辅助gettxt翻译的vim配置

看图吧,用得到的话会觉的简直太爽了,^_^
使用方法:
打开需要标记的文件,输入命令 :GettextMarkStart,开启辅助
选择需要翻译的文本
screenshot_092.png
按_出来提示菜单,选择完成标记
screenshot_093.png

配置:http://code-of-emptyhua.googlecode.com/svn/trunk/vim/addgettextmark.vim

———–
post by gmail~

相对完美的设计思路~

有没有可能写一套代码适应以下三种情况:
1. 客户端没有开启javascript,或者使用ucweb一类的弱智浏览器访问时,功能没有影响
2. 客户端开启了javascript,每个页面都有单独加载的js,但是禁用ajax,只帮助用户交互
3. 客户端开启了javascript,为了提高载入速度,尽量使所有操作用ajax完成

略有所得: http://www.blogo2.com
源代码: http://code-of-emptyhua.googlecode.com/svn/trunk/blogo2

没有禁用js的时候,页面切换,表单提交(注册和登录没有用)尽量的使用ajax,而且部分操作会使用弹层(登录后的管理),禁用js之后,则所有操作换成普通表单提交在页面之间跳来跳去。。

还有一个功能没有开启,就是可以选择不用ajax,但是独立开的页面js依然有效。

有兴趣的同学可以check源代码,这种模式的实现:
1. 开发的时候禁用ajax,单个页面开发,而且要求禁用js时业务可以走通,页面内的js和css统一由模板引入。
2. 对需要开启ajax的链接增加hook,例如data-ajax=”dialog:{}”,则表明在开启ajax模式后,点击这个链接需要弹窗
3. ajax模式下只需要对单个页面更换特有的模板,动态加载需要的js和css即可。

这种设计的特点:
1. 尽量一个人包前后端。。
2. json不潮了,ajax返回html代码,优点是js不用套页面了,缺点是流量大

over,虽然没有完美的代码,但是我尽量~

———–
post by gmail~