Skip to content

Instantly share code, notes, and snippets.

@jeremyroman
Created December 22, 2016 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyroman/9c6445fcf988a88e9440c275f2471afe to your computer and use it in GitHub Desktop.
Save jeremyroman/9c6445fcf988a88e9440c275f2471afe to your computer and use it in GitHub Desktop.
WDYT of templating APISignature::ParseArgumentsImpl
namespace {
class V8ParsingBuffer {
public:
V8ParsingBuffer(std::vector<v8::Local<v8::Value>> values, v8::Isolate* isolate)
: values_(values), isolate_(isolate) {}
void AddNull() { values_.push_back(v8::Null(isolate)); }
std::nullptr_t GetParsedArgumentBuffer() { return nullptr; }
void AddParsedArgument(v8::Local<v8::Value> value) { values_->push_back(value); }
private:
v8::Isolate* isolate_;
std::vector<v8::Local<v8::Value>>* values_;
};
} // namespace
bool APISignature::ParseArgumentsToV8(gin::Arguments* arguments,
const ArgumentSpec::RefMap& type_refs,
std::vector<v8::Local<v8::Value>>* v8_out,
std::string* error) const {
std::vector<v8::Local<v8::Value>> v8_values;
v8::Local<v8::Function> callback;
V8ParsingBuffer buffer(&v8_values, arguments->isolate());
if (!ParseArgumentsImpl(arguments, type_refs, &buffer, error))
return false;
if (!callback.IsEmpty())
v8_values->push_back(callback);
*v8_out = std::move(v8_values);
return true;
}
namespace {
class BaseValueParsingBuffer {
public:
explicit BaseValueParsingBuffer(base::ListValue* values) : values_(values) {}
void AddNull() { values_->Append(base::Value::CreateNullValue()); }
std::unique_ptr<base::Value>* GetParsedArgumentBuffer() { return &last_arg_; }
// The corresponding base::Value is expected to have been stored in
// |GetParsedArgumentBuffer()| already.
void AddParsedArgument(v8::Local<v8::Value>) { values_->Append(std::move(last_arg)); }
private:
base::ListValue* values_;
std::unique_ptr<base::Value> last_arg_;
};
} // namespace
bool APISignature::ParseArgumentsToJSON(gin::Arguments* arguments,
const ArgumentSpec::RefMap& type_refs,
std::unique_ptr<base::ListValue>* json_out,
v8::Local<v8::Function>* callback_out,
std::string* error) const {
std::unique_ptr<base::ListValue> json = base::MakeUnique<base::ListValue>();
v8::Local<v8::Function> callback;
BaseValueParsingBuffer buffer(json.get());
if (!ParseArgumentsImpl(arguments, type_refs, &buffer, error))
return false;
*json_out = std::move(json);
*callback_out = callback;
return true;
}
template <typename ParsingBuffer>
bool APISignature::ParseArgumentsImpl(
gin::Arguments* arguments,
const ArgumentSpec::RefMap& type_refs,
ParsingBuffer* arguments_out,
v8::Local<v8::Function>* callback_out,
std::string* error) const {
DCHECK(arguments_out && callback_out && error);
bool signature_has_callback = ...;
// pass |arguments_out| to ParseArguments instead of v8_results and
// json_results; pass |callback_out| directly to ParseCallback
return true;
}
template <typename ParsingBuffer>
bool APISignature::ParseArgument(const ArgumentSpec& spec,
v8::Local<v8::Context> context,
gin::Arguments* arguments,
const ArgumentSpec::RefMap& type_refs,
ParsingBuffer* arguments_out,
std::string* error) const {
// same, but using
// arguments_out->AddNull(); to add null
// arguments_out->GetParsedArgumentBuffer(); to pass to ArgumentSpec::ParseArgument
// arguments_out->AddParsedArgument(value); to store a provided argument
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment