To parse the parameters of the function that I get from JavaScript, I need to do a lot of checks. For example, a function can expect an object as a parameter that looks like this in JavaScript.
{ Fullscreen: [ 'bool', false ], Size: [ 'Vector2u', 800, 600 ], Title: [ 'string', 'Hello World' ], // more properties... }
In C ++, I parse this by looping through all the keys and checking them. If one of these checks fails, an error message should be printed and this pair of key values should be skipped. Here's what my implementation looks like at the moment. I hope you do not get distracted by some of the challenges associated with the engine.
ModuleSettings *module = (ModuleSettings*)HelperScript::Unwrap(args.Data()); if(args.Length() < 1 || !args[0]->IsObject()) return v8::Undefined(); v8::Handle<v8::Object> object = args[0]->ToObject(); auto stg = module->Global->Get<Settings>("settings"); v8::Handle<v8::Array> keys = object->GetPropertyNames(); for(unsigned int i = 0; i < keys->Length(); ++i) { string key = *v8::String::Utf8Value(keys->Get(i)); if(!object->Get(v8::String::New(key.c_str()))->IsArray()) { HelperDebug::Fail("script", "could not parse (" + key + ") setting"); continue; } v8::Handle<v8::Array> values = v8::Handle<v8::Array>::Cast(object->Get(v8::String::New(key.c_str()))); if(!values->Has(0) || !values->Get(0)->IsString()) { HelperDebug::Fail("script", "could not parse (" + key + ") setting"); continue; } string type = *v8::String::Utf8Value(values->Get(0)); if(type == "bool") { if(!values->Has(1) || !values->Get(1)->IsBoolean()) { HelperDebug::Fail("script", "could not parse (" + key + ") setting"); continue; } stg->Set<bool>(key, values->Get(1)->BooleanValue()); } else if(type == "Vector2u") { if(!values->Has(1) || !values->Has(2) || !values->Get(1)->IsUint32(), !values->Get(2)->IsUint32()) { HelperDebug::Fail("script", "could not parse (" + key + ") setting"); continue; } stg->Set<Vector2u>(key, Vector2u(values->Get(1)->Uint32Value(), values->Get(2)->Uint32Value())); } else if(type == "string") { if(!values->Has(1) || !values->Get(1)->IsString()) { HelperDebug::Fail("script", "could not parse (" + key + ") setting"); continue; } stg->Set<string>(key, *v8::String::Utf8Value(values->Get(1))); } }
As you can see, I determined when this happens, when the check fails with each filter.
HelperDebug::Fail("script", "could not parse (" + key + ") setting"); continue;
I would like to write this only once, but I can only come up with a way using goto that I would like to prevent. Is there a better option for restructuring the if else ?