NativeClient

今頃NativeClientかよっ、って感じもしますが、XtalもちゃんとNativeClientで動かせるのか気になったので試してみました。


C++ソース test.cpp

#include "../src/xtal/xtal_all_src.h"

#include <nacl/nacl_srpc.h>

int doxtal(NaClSrpcChannel *channel, NaClSrpcArg **in_args, NaClSrpcArg **out_args){
	using namespace xtal;
	
	struct Guard{
		Guard(){ 
			Setting setting;
			initialize(setting); 
		}	
		~Guard(){ uninitialize(); }	
	} guard;
	
	if(CodePtr code = compile(in_args[0]->u.sval)){		
		AnyPtr ret = code->call();
		
		XTAL_CATCH_EXCEPT(e){
			out_args[0]->u.sval = strdup(e->to_s()->c_str());
			return NACL_SRPC_RESULT_OK;
		}
		
		out_args[0]->u.sval = strdup(ret->to_s()->c_str());
		return NACL_SRPC_RESULT_OK;
	}
	
	XTAL_CATCH_EXCEPT(e){
		out_args[0]->u.sval = strdup(e->to_s()->c_str());
		return NACL_SRPC_RESULT_OK;
	}

	return NACL_SRPC_RESULT_APP_ERROR;
}

NACL_SRPC_METHOD("xtal:s:s", doxtal);

表示する test.html

<html>
<body>
<textarea id="src">
return 10 + 20;
</textarea>
<embed id="nacl" type="application/x-nacl-srpc" width="0" height="0" src="xtal.nexe" />
<a href="javascript:void(0);" onclick="alert(document.getElementById('nacl').xtal(document.getElementById('src').value))"
<p>do</p></a>
</body>
</html>

上の二つを保存して

nacl-g++ -lsrpc -lpthread -static test.cpp -o xtal.nexe

コンパイルします。
そしてtest.htmlを開くとちゃんとFirefox上入力したXtalソースをコンパイル、実行して結果を表示することができました。