Commit 2d91b4aa authored by Scott Graham's avatar Scott Graham Committed by Commit Bot

fuchsia: Simplify exposed bindings names

Also, remove channelCreate and handleClose since they aren't yet useful,
and we'll replace them with something better soon (MessagePort,
Transferrable, etc.)

Bug: 883496
Change-Id: I3861398035e79029cdbde01cc9a4ce4ea872ab0b
Reviewed-on: https://chromium-review.googlesource.com/c/1292721Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601999}
parent adedcd1d
...@@ -335,7 +335,7 @@ function %(name)s() {} ...@@ -335,7 +335,7 @@ function %(name)s() {}
* @implements %(name)s * @implements %(name)s
*/ */
function %(proxy_name)s() { function %(proxy_name)s() {
this.channel = zx.ZX_HANDLE_INVALID; this.channel = $ZX_HANDLE_INVALID;
} }
%(proxy_name)s.prototype.$bind = function(channel) { %(proxy_name)s.prototype.$bind = function(channel) {
...@@ -356,7 +356,7 @@ function %(proxy_name)s() { ...@@ -356,7 +356,7 @@ function %(proxy_name)s() {
self.f.write( self.f.write(
'''\ '''\
%(proxy_name)s.prototype.%(method_name)s = function(%(param_names)s) { %(proxy_name)s.prototype.%(method_name)s = function(%(param_names)s) {
if (this.channel === zx.ZX_HANDLE_INVALID) { if (this.channel === $ZX_HANDLE_INVALID) {
throw "channel closed"; throw "channel closed";
} }
var $encoder = new $fidl_Encoder(_k%(name)s_%(method_name)s_Ordinal); var $encoder = new $fidl_Encoder(_k%(name)s_%(method_name)s_Ordinal);
...@@ -379,11 +379,11 @@ function %(proxy_name)s() { ...@@ -379,11 +379,11 @@ function %(proxy_name)s() {
'offset': param.offset 'offset': param.offset
}) })
self.f.write(''' var $writeResult = zx.channelWrite(this.channel, self.f.write(''' var $writeResult = $ZxChannelWrite(this.channel,
$encoder.messageData(), $encoder.messageData(),
$encoder.messageHandles()); $encoder.messageHandles());
if ($writeResult !== zx.ZX_OK) { if ($writeResult !== $ZX_OK) {
throw "zx.channelWrite failed: " + $writeResult; throw "$ZxChannelWrite failed: " + $writeResult;
} }
''') ''')
...@@ -392,11 +392,10 @@ function %(proxy_name)s() { ...@@ -392,11 +392,10 @@ function %(proxy_name)s() {
for param in method.maybe_response: for param in method.maybe_response:
type_tables.append(self._CompileType(param.type)) type_tables.append(self._CompileType(param.type))
self.f.write(''' self.f.write('''
return zx return $ZxObjectWaitOne(this.channel, $ZX_CHANNEL_READABLE, $ZX_TIME_INFINITE)
.objectWaitOne(this.channel, zx.ZX_CHANNEL_READABLE, zx.ZX_TIME_INFINITE)
.then(() => new Promise(res => { .then(() => new Promise(res => {
var $readResult = zx.channelRead(this.channel); var $readResult = $ZxChannelRead(this.channel);
if ($readResult.status !== zx.ZX_OK) { if ($readResult.status !== $ZX_OK) {
throw "channel read failed"; throw "channel read failed";
} }
......
...@@ -162,7 +162,7 @@ const _kTT_String_Nonnull = { ...@@ -162,7 +162,7 @@ const _kTT_String_Nonnull = {
if (v === null || v === undefined) throw "non-null string required"; if (v === null || v === undefined) throw "non-null string required";
// Both size and data are uint64, but that's awkward in JS, so for now only // Both size and data are uint64, but that's awkward in JS, so for now only
// support a maximum of 32b lengths. // support a maximum of 32b lengths.
var asUtf8 = zx.strToUtf8Array(v); var asUtf8 = $FidlJsStrToUtf8Array(v);
e.data.setUint32(o, asUtf8.length, $fidl__kLE); e.data.setUint32(o, asUtf8.length, $fidl__kLE);
e.data.setUint32(o + 4, 0, $fidl__kLE); e.data.setUint32(o + 4, 0, $fidl__kLE);
e.data.setUint32(o + 8, 0xffffffff, $fidl__kLE); e.data.setUint32(o + 8, 0xffffffff, $fidl__kLE);
...@@ -174,7 +174,7 @@ const _kTT_String_Nonnull = { ...@@ -174,7 +174,7 @@ const _kTT_String_Nonnull = {
var pointer = d.data.getUint32(o + 8, $fidl__kLE); var pointer = d.data.getUint32(o + 8, $fidl__kLE);
if (pointer === 0) throw "non-null string required"; if (pointer === 0) throw "non-null string required";
var dataOffset = d.claimMemory(len); var dataOffset = d.claimMemory(len);
return zx.utf8ArrayToStr(new DataView(d.data.buffer, dataOffset, len)); return $FidlJsUtf8ArrayToStr(new DataView(d.data.buffer, dataOffset, len));
} }
}; };
......
...@@ -189,16 +189,6 @@ v8::Local<v8::Promise> ZxObjectWaitOne(gin::Arguments* args) { ...@@ -189,16 +189,6 @@ v8::Local<v8::Promise> ZxObjectWaitOne(gin::Arguments* args) {
return v8::Local<v8::Promise>(); return v8::Local<v8::Promise>();
} }
v8::Local<v8::Object> ZxChannelCreate(v8::Isolate* isolate) {
zx::channel c1, c2;
zx_status_t status = zx::channel::create(0, &c1, &c2);
return gin::DataObjectBuilder(isolate)
.Set("status", status)
.Set("first", c1.release())
.Set("second", c2.release())
.Build();
}
zx_status_t ZxChannelWrite(gin::Arguments* args) { zx_status_t ZxChannelWrite(gin::Arguments* args) {
zx_handle_t handle; zx_handle_t handle;
if (!args->GetNext(&handle)) { if (!args->GetNext(&handle)) {
...@@ -295,19 +285,6 @@ v8::Local<v8::Value> Utf8ArrayToStr(gin::Arguments* args) { ...@@ -295,19 +285,6 @@ v8::Local<v8::Value> Utf8ArrayToStr(gin::Arguments* args) {
data.num_bytes())); data.num_bytes()));
} }
v8::Local<v8::Object> GetOrCreateZxObject(v8::Isolate* isolate,
v8::Local<v8::Object> global) {
v8::Local<v8::Object> zx;
v8::Local<v8::Value> zx_value = global->Get(gin::StringToV8(isolate, "zx"));
if (zx_value.IsEmpty() || !zx_value->IsObject()) {
zx = v8::Object::New(isolate);
global->Set(gin::StringToSymbol(isolate, "zx"), zx);
} else {
zx = v8::Local<v8::Object>::Cast(zx);
}
return zx;
}
} // namespace } // namespace
namespace fidljs { namespace fidljs {
...@@ -317,10 +294,9 @@ ZxBindings::ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global) ...@@ -317,10 +294,9 @@ ZxBindings::ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global)
DCHECK_EQ(isolate->GetData(gin::kEmbedderFuchsia), nullptr); DCHECK_EQ(isolate->GetData(gin::kEmbedderFuchsia), nullptr);
isolate->SetData(gin::kEmbedderFuchsia, wait_set_.get()); isolate->SetData(gin::kEmbedderFuchsia, wait_set_.get());
v8::Local<v8::Object> zx = GetOrCreateZxObject(isolate, global); #define SET_CONSTANT(k) \
global->Set(gin::StringToSymbol(isolate, "$" #k), \
#define SET_CONSTANT(k) \ gin::ConvertToV8(isolate, k))
zx->Set(gin::StringToSymbol(isolate, #k), gin::ConvertToV8(isolate, k))
// zx_status_t. // zx_status_t.
SET_CONSTANT(ZX_OK); SET_CONSTANT(ZX_OK);
...@@ -371,29 +347,20 @@ ZxBindings::ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global) ...@@ -371,29 +347,20 @@ ZxBindings::ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global)
SET_CONSTANT(ZX_ERR_CONNECTION_ABORTED); SET_CONSTANT(ZX_ERR_CONNECTION_ABORTED);
// Handle APIs. // Handle APIs.
zx->Set(gin::StringToSymbol(isolate, "handleClose"), global->Set(
gin::CreateFunctionTemplate(isolate, gin::StringToSymbol(isolate, "$ZxObjectWaitOne"),
base::BindRepeating(&zx_handle_close))
->GetFunction());
SET_CONSTANT(ZX_HANDLE_INVALID);
zx->Set(
gin::StringToSymbol(isolate, "objectWaitOne"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(ZxObjectWaitOne)) gin::CreateFunctionTemplate(isolate, base::BindRepeating(ZxObjectWaitOne))
->GetFunction()); ->GetFunction());
SET_CONSTANT(ZX_HANDLE_INVALID); SET_CONSTANT(ZX_HANDLE_INVALID);
SET_CONSTANT(ZX_TIME_INFINITE); SET_CONSTANT(ZX_TIME_INFINITE);
// Channel APIs. // Channel APIs.
zx->Set(gin::StringToSymbol(isolate, "channelCreate"), global->Set(
gin::CreateFunctionTemplate(isolate, gin::StringToSymbol(isolate, "$ZxChannelWrite"),
base::BindRepeating(&ZxChannelCreate))
->GetFunction());
zx->Set(
gin::StringToSymbol(isolate, "channelWrite"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(&ZxChannelWrite)) gin::CreateFunctionTemplate(isolate, base::BindRepeating(&ZxChannelWrite))
->GetFunction()); ->GetFunction());
zx->Set( global->Set(
gin::StringToSymbol(isolate, "channelRead"), gin::StringToSymbol(isolate, "$ZxChannelRead"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(&ZxChannelRead)) gin::CreateFunctionTemplate(isolate, base::BindRepeating(&ZxChannelRead))
->GetFunction()); ->GetFunction());
SET_CONSTANT(ZX_CHANNEL_READABLE); SET_CONSTANT(ZX_CHANNEL_READABLE);
...@@ -405,14 +372,12 @@ ZxBindings::ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global) ...@@ -405,14 +372,12 @@ ZxBindings::ZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global)
// Utilities to make string handling easier to convert to/from UCS-2 (JS) <-> // Utilities to make string handling easier to convert to/from UCS-2 (JS) <->
// UTF-8 (FIDL). // UTF-8 (FIDL).
// TODO(crbug.com/883496): This is not really zx, should move to a generic global->Set(
// runtime helper file if there are more similar C++ helpers required. gin::StringToSymbol(isolate, "$FidlJsStrToUtf8Array"),
zx->Set(
gin::StringToSymbol(isolate, "strToUtf8Array"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(&StrToUtf8Array)) gin::CreateFunctionTemplate(isolate, base::BindRepeating(&StrToUtf8Array))
->GetFunction()); ->GetFunction());
zx->Set( global->Set(
gin::StringToSymbol(isolate, "utf8ArrayToStr"), gin::StringToSymbol(isolate, "$FidlJsUtf8ArrayToStr"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(&Utf8ArrayToStr)) gin::CreateFunctionTemplate(isolate, base::BindRepeating(&Utf8ArrayToStr))
->GetFunction()); ->GetFunction());
......
...@@ -127,33 +127,6 @@ class BindingsSetupHelper { ...@@ -127,33 +127,6 @@ class BindingsSetupHelper {
DISALLOW_COPY_AND_ASSIGN(BindingsSetupHelper); DISALLOW_COPY_AND_ASSIGN(BindingsSetupHelper);
}; };
TEST_F(FidlGenJsTest, CreateChannelPair) {
v8::Isolate* isolate = instance_->isolate();
BindingsSetupHelper helper(isolate);
std::string source = R"(
var result = zx.channelCreate();
this.result_status = result.status;
this.result_h1 = result.first;
this.result_h2 = result.second;
if (result.status == zx.ZX_OK) {
zx.handleClose(result.first);
zx.handleClose(result.second);
}
)";
helper.runner().Run(source, "test.js");
auto status = helper.Get<zx_status_t>("result_status");
EXPECT_EQ(status, ZX_OK);
auto h1 = helper.Get<zx_handle_t>("result_h1");
EXPECT_NE(h1, ZX_HANDLE_INVALID);
auto h2 = helper.Get<zx_handle_t>("result_h2");
EXPECT_NE(h2, ZX_HANDLE_INVALID);
}
class TestolaImpl : public fidljstest::Testola { class TestolaImpl : public fidljstest::Testola {
public: public:
TestolaImpl() { TestolaImpl() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment