diff --git a/bindings/NodeJS/binding.gyp b/bindings/NodeJS/binding.gyp index 99abf0e..63d2da1 100644 --- a/bindings/NodeJS/binding.gyp +++ b/bindings/NodeJS/binding.gyp @@ -2,7 +2,8 @@ "targets": [ { "target_name": "judger", - "sources": [ "judger.cc"] + 'cflags_cc!': [ '-std=c++11','-O2' ], + "sources": [ "judger.cc"], } ] } diff --git a/bindings/NodeJS/judger.cc b/bindings/NodeJS/judger.cc index 0bfcb63..a386e1b 100644 --- a/bindings/NodeJS/judger.cc +++ b/bindings/NodeJS/judger.cc @@ -26,21 +26,22 @@ namespace demo { using v8::Context; /* 转成数字 */ - void ToNumber(Isolate * isolate,Local &args,char * key,std::string &str){ + bool ToNumber(Isolate * isolate,Local &args,char * key,std::string &str){ Local context= isolate->GetCurrentContext(); Local val = args->ToObject()->Get(context,String::NewFromUtf8(isolate,key)).ToLocalChecked(); if(val->IsNullOrUndefined()) - return ; + return true; else if( val->IsNumber()){ Local num = val->ToInteger(context).ToLocalChecked(); str+=" --"; str+=std::string(key); str+="="; str+=std::to_string(num->Value()); - return ; + return true; } else { isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"typeof argument must be Object!"))); + return false; } } @@ -51,7 +52,7 @@ namespace demo { } /* 转成字符串 */ - void ToCStr(Isolate * isolate,Local &args,char * key,std::string &str){ + bool ToCStr(Isolate * isolate,Local &args,char * key,std::string &str){ char str_content[255]={0}; Local context= isolate->GetCurrentContext(); @@ -61,19 +62,20 @@ namespace demo { else val = args; if(val->IsNullOrUndefined()) - return; + return true; else if(val->IsString()){ _ToCharPTR(val,context,str_content); str+=" --"; str+=std::string(key); str+="="; str+=std::string(str_content); - return ; + return true; } else { char ret[100]; sprintf(ret,"typeof %s must be String!",key); isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,ret))); + return false; } } @@ -120,47 +122,23 @@ namespace demo { Local argument = args[0]; + std::string int_vars[] = { "max_cpu_time","max_real_time","max_memory","memory_limit_check_only","max_stack","max_process_number","max_output_size","uid","gid"}; + std::string str_vars[] = { "input_path","output_path","error_path","exe_path","log_path","seccomp_rule_name"}; - /* max_cpu_time */ - ToNumber(isolate,argument,(char *)"max_cpu_time",_args); + for( auto var :int_vars){ + if( ! ToNumber(isolate,argument,(char *)var.c_str(),_args)) + return; + } - /* max_real_time */ - ToNumber(isolate,argument,(char *)"max_real_time",_args); - - /* max_memory */ - ToNumber(isolate,argument,(char *)"max_memory",_args); - - /* memory_limit_check_only */ - ToNumber(isolate,argument,(char *)"memory_limit_check_only",_args); - - - - /* max_stack */ - ToNumber(isolate,argument,(char *)"max_stack",_args); //默认16mb - - /* max_process_number */ - ToNumber(isolate,argument,(char *)"max_process_number",_args); - - /* max_output_size */ - ToNumber(isolate,argument,(char *)"max_output_size",_args); - - /* input_path */ - ToCStr(isolate,argument,(char *)"input_path",_args); - - /* output_path */ - ToCStr(isolate,argument,(char *)"output_path",_args); - - /* error_path */ - ToCStr(isolate,argument,(char *)"error_path",_args); - - /* exe_path */ - ToCStr(isolate,argument,(char *)"exe_path",_args); + for( auto var : str_vars){ + if( !ToCStr(isolate,argument,(char *)var.c_str(),_args)) + return; + } /* args */ - Local margs= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"args")).ToLocalChecked(); if( margs->IsNullOrUndefined()){ - NULL; + ; } else if( margs->IsArray()){ Local args = margs.As(); @@ -168,16 +146,18 @@ namespace demo { int i; for(i=0;i in = args->Get(i); - ToCStr(isolate,in,(char *)"args",_args); + if( !ToCStr(isolate,in,(char *)"args",_args)) + return; } } else { //not array isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"args must be a list"))); + return; } Local menv= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"env")).ToLocalChecked(); if( menv->IsNullOrUndefined()){ - NULL; + ; } else if( margs->IsArray()){ Local env = menv.As(); @@ -185,24 +165,16 @@ namespace demo { int i; for(i=0;i in = env->Get(i); - ToCStr(isolate,in,(char *)"env",_args); + if(!ToCStr(isolate,in,(char *)"env",_args)) + return; + } } else { //not array isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"env must be a list"))); + return; } - /* log_path */ - ToCStr(isolate,argument,(char *)"log_path",_args); - - /* seccomp_rule_name */ - char seccomp_rule_name[255]; - ToCStr(isolate,argument,(char *)"seccomp_rule_name",_args); - - /* uid */ - ToNumber(isolate,argument,(char *)"uid",_args); - /* gid */ - ToNumber(isolate,argument,(char *)"gid",_args); char buf[255]; @@ -222,7 +194,8 @@ namespace demo { break; } - printf("%*.*s",len,len,pr); //输出 程序的输出 + if(len) + printf("%*.*s",len,len,pr); //输出 程序的输出 MaybeLocal mres = JSON::Parse(isolate,String::NewFromUtf8(isolate,pr+len)); args.GetReturnValue().Set(mres.ToLocalChecked()); diff --git a/bindings/NodeJS/package.json b/bindings/NodeJS/package.json index 1e99a7a..6390aaf 100644 --- a/bindings/NodeJS/package.json +++ b/bindings/NodeJS/package.json @@ -1,12 +1,16 @@ { - "name": "judger", - "version": "1.0.0", - "description": "judger for online system", - "main": "./build/Release/judger.node", - "scripts": { - "build":"node-gyp rebuild", - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "virusdefender,rainboy", - "license": "GPL" + "name": "judger", + "version": "1.0.0", + "description": "judger for online system", + "main": "./build/Release/judger.node", + "scripts": { + "build": "node-gyp rebuild", + "test": "mocha -t 5000 ../../tests/Nodejs_and_core" + }, + "author": "virusdefender,rainboy", + "license": "GPL", + "devDependencies": { + "mocha": "^6.1.4", + "node-gyp": "^4.0.0" + } }