Skip to content

Instantly share code, notes, and snippets.

@h2oota
Last active December 16, 2015 19:30
Show Gist options
  • Save h2oota/5485926 to your computer and use it in GitHub Desktop.
Save h2oota/5485926 to your computer and use it in GitHub Desktop.
c++/cliからJScriptを呼び出します。 http://www.west-wind.com/WebLog/posts/10688.aspx (消滅)のコメント(by Lea Hayes October 01, 2008 @ 9:32 am)と 参照されているページ(http://odetocode.com/articles/80.aspx)を参考にしました。
ref class Evaluator
{
public:
static System::Object^ Eval(System::String ^statement) {
return _evaluatorType->InvokeMember(
"Eval",
System::Reflection::BindingFlags::InvokeMethod,
nullptr, _evaluator,
gcnew array<System::Object^>(1) { statement });
}
static System::Object^ EvalFunc(
System::String ^func, System::Object ^args) {
System::Runtime::Serialization::Json
::DataContractJsonSerializerSettings flags;
flags.UseSimpleDictionaryFormat = true;
System::Runtime::Serialization::Json::DataContractJsonSerializer
ser(args->GetType(), %flags);
System::IO::MemoryStream s;
s.WriteByte('(');
ser.WriteObject(%s, args);
s.WriteByte(')');
s.Flush();
return _evaluatorType->InvokeMember(
"EvalFunc",
System::Reflection::BindingFlags::InvokeMethod,
nullptr, _evaluator,
gcnew array<System::Object^>(2) {
"(" + func + ")",
System::Text::Encoding::Default->GetString(s.ToArray())});
}
private:
Evaluator() { }
static System::Type ^ compileEvaluator() {
Microsoft::JScript::JScriptCodeProvider compiler;
System::CodeDom::Compiler::CompilerParameters parameters;
parameters.GenerateInMemory = true;
#define _ "\n"
return
compiler.CompileAssemblyFromSource(
%parameters,
"package Evaluator {" _
" class Evaluator {" _
" function Eval(expr : String)" _
" {" _
" return eval(expr);" _
" }" _
" function EvalFunc(func : String, arg : String)" _
" {" _
" return eval(func)(eval(arg));" _
" }" _
" }" _
"}")->CompiledAssembly->GetType("Evaluator.Evaluator");
#undef _
}
static System::Type ^ const _evaluatorType = compileEvaluator();
static System::Object ^ const _evaluator =
System::Activator::CreateInstance(_evaluatorType);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment