upgrade code format

This commit is contained in:
rainboy 2019-04-26 15:09:43 +08:00
parent d54b84ab58
commit d6a3151506
3 changed files with 45 additions and 67 deletions

View File

@ -2,7 +2,8 @@
"targets": [ "targets": [
{ {
"target_name": "judger", "target_name": "judger",
"sources": [ "judger.cc"] 'cflags_cc!': [ '-std=c++11','-O2' ],
"sources": [ "judger.cc"],
} }
] ]
} }

View File

@ -26,21 +26,22 @@ namespace demo {
using v8::Context; using v8::Context;
/* 转成数字 */ /* 转成数字 */
void ToNumber(Isolate * isolate,Local<Value> &args,char * key,std::string &str){ bool ToNumber(Isolate * isolate,Local<Value> &args,char * key,std::string &str){
Local<Context> context= isolate->GetCurrentContext(); Local<Context> context= isolate->GetCurrentContext();
Local<Value> val = args->ToObject()->Get(context,String::NewFromUtf8(isolate,key)).ToLocalChecked(); Local<Value> val = args->ToObject()->Get(context,String::NewFromUtf8(isolate,key)).ToLocalChecked();
if(val->IsNullOrUndefined()) if(val->IsNullOrUndefined())
return ; return true;
else if( val->IsNumber()){ else if( val->IsNumber()){
Local<Integer> num = val->ToInteger(context).ToLocalChecked(); Local<Integer> num = val->ToInteger(context).ToLocalChecked();
str+=" --"; str+=" --";
str+=std::string(key); str+=std::string(key);
str+="="; str+="=";
str+=std::to_string(num->Value()); str+=std::to_string(num->Value());
return ; return true;
} }
else { else {
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"typeof argument must be Object!"))); 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<Value> &args,char * key,std::string &str){ bool ToCStr(Isolate * isolate,Local<Value> &args,char * key,std::string &str){
char str_content[255]={0}; char str_content[255]={0};
Local<Context> context= isolate->GetCurrentContext(); Local<Context> context= isolate->GetCurrentContext();
@ -61,19 +62,20 @@ namespace demo {
else else
val = args; val = args;
if(val->IsNullOrUndefined()) if(val->IsNullOrUndefined())
return; return true;
else if(val->IsString()){ else if(val->IsString()){
_ToCharPTR(val,context,str_content); _ToCharPTR(val,context,str_content);
str+=" --"; str+=" --";
str+=std::string(key); str+=std::string(key);
str+="="; str+="=";
str+=std::string(str_content); str+=std::string(str_content);
return ; return true;
} }
else { else {
char ret[100]; char ret[100];
sprintf(ret,"typeof %s must be String!",key); sprintf(ret,"typeof %s must be String!",key);
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,ret))); isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,ret)));
return false;
} }
} }
@ -120,47 +122,23 @@ namespace demo {
Local<Value> argument = args[0]; Local<Value> 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 */ for( auto var :int_vars){
ToNumber(isolate,argument,(char *)"max_cpu_time",_args); if( ! ToNumber(isolate,argument,(char *)var.c_str(),_args))
return;
}
/* max_real_time */ for( auto var : str_vars){
ToNumber(isolate,argument,(char *)"max_real_time",_args); if( !ToCStr(isolate,argument,(char *)var.c_str(),_args))
return;
/* 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);
/* args */ /* args */
Local<Value> margs= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"args")).ToLocalChecked(); Local<Value> margs= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"args")).ToLocalChecked();
if( margs->IsNullOrUndefined()){ if( margs->IsNullOrUndefined()){
NULL; ;
} }
else if( margs->IsArray()){ else if( margs->IsArray()){
Local<Array> args = margs.As<Array>(); Local<Array> args = margs.As<Array>();
@ -168,16 +146,18 @@ namespace demo {
int i; int i;
for(i=0;i<len;i++){ for(i=0;i<len;i++){
Local<Value> in = args->Get(i); Local<Value> in = args->Get(i);
ToCStr(isolate,in,(char *)"args",_args); if( !ToCStr(isolate,in,(char *)"args",_args))
return;
} }
} }
else { //not array else { //not array
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"args must be a list"))); isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"args must be a list")));
return;
} }
Local<Value> menv= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"env")).ToLocalChecked(); Local<Value> menv= argument->ToObject()->Get(context,String::NewFromUtf8(isolate,"env")).ToLocalChecked();
if( menv->IsNullOrUndefined()){ if( menv->IsNullOrUndefined()){
NULL; ;
} }
else if( margs->IsArray()){ else if( margs->IsArray()){
Local<Array> env = menv.As<Array>(); Local<Array> env = menv.As<Array>();
@ -185,24 +165,16 @@ namespace demo {
int i; int i;
for(i=0;i<len;i++){ for(i=0;i<len;i++){
Local<Value> in = env->Get(i); Local<Value> in = env->Get(i);
ToCStr(isolate,in,(char *)"env",_args); if(!ToCStr(isolate,in,(char *)"env",_args))
return;
} }
} }
else { //not array else { //not array
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate,"env must be a list"))); 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]; char buf[255];
@ -222,7 +194,8 @@ namespace demo {
break; break;
} }
printf("%*.*s",len,len,pr); //输出 程序的输出 if(len)
printf("%*.*s",len,len,pr); //输出 程序的输出
MaybeLocal<Value> mres = JSON::Parse(isolate,String::NewFromUtf8(isolate,pr+len)); MaybeLocal<Value> mres = JSON::Parse(isolate,String::NewFromUtf8(isolate,pr+len));
args.GetReturnValue().Set(mres.ToLocalChecked()); args.GetReturnValue().Set(mres.ToLocalChecked());

View File

@ -1,12 +1,16 @@
{ {
"name": "judger", "name": "judger",
"version": "1.0.0", "version": "1.0.0",
"description": "judger for online system", "description": "judger for online system",
"main": "./build/Release/judger.node", "main": "./build/Release/judger.node",
"scripts": { "scripts": {
"build":"node-gyp rebuild", "build": "node-gyp rebuild",
"test": "echo \"Error: no test specified\" && exit 1" "test": "mocha -t 5000 ../../tests/Nodejs_and_core"
}, },
"author": "virusdefender,rainboy", "author": "virusdefender,rainboy",
"license": "GPL" "license": "GPL",
"devDependencies": {
"mocha": "^6.1.4",
"node-gyp": "^4.0.0"
}
} }