Project

General

Profile

Feature #21660

Updated by Richard Neswold over 1 year ago

h2. @class@ syntax

Change the Javascript output to use the @class@ keyword syntax for messages. For example, the following source file:

<pre>
request example {
string name;
int32 age;
}
</pre>

generates:

<pre><code class="js">
var JUNK_request_example = function () {
this.name = "";
this.age = 0;
};

JUNK_request_example.prototype.marshal = function* () {
yield* [83, 68, 68, 2, 81, 3, 20, 12, 130, 31, 103, 18, 245, 24, 81, 4];
yield* [18, 147, 28];
yield* PROTOCOL.m_string(this.name);
yield* [18, 125, 39];
yield* PROTOCOL.m_int(this.age);
};
</code></pre>

With the new generator, it should look something like:

<pre><code class="js">
class JUNK_request_example {
constructor () {
this.name = "";
this.age = 0;
}

*marshal() {
yield* [83, 68, 68, 2, 81, 3, 20, 12, 130, 31, 103, 18, 245, 24, 81, 4];
yield* [18, 147, 28];
yield* PROTOCOL.m_string(this.name);
yield* [18, 125, 39];
yield* PROTOCOL.m_int(this.age);
}
};
</code></pre>

h2. Using Inheritance

The code can use minimal inheritance features to logically tie all requests together and all replies together. Requests and replies would be derived from a simple request and reply base class, respectively, that only has a static method to unmarshal messages of that type.

In the case of the request base class, we would also add to its @prototype@ property a reference to the reply base class' unmarshal method. Currently, when the ACNET Javascript code is given a message to send, it checks to see if the message object has a @marshal()@ method method, and, if of so, uses it to encode the message. The ACNET module could also use this extra property to automatically unmarshal replies before delivering them to the associated callback.

<pre><code class="js">
// Start of Request hierarchy.

class JUNK_requestbase {
static unmarshal(o) {
...
}
}

class JUNK_request_example extends JUNK_requestbase {
...
};

// Start of Reply hierarchy.

class JUNK_replybase {
static unmarshal(o) {
...
}
}

// Now that the reply base class is defined, we can grab its unmarshal function
// and associate it with requests.

Object.defineProperty(JUNK_requestbase, 'unmarshalReply', { value: JUNK_replybase.unmarshal });

// ... other Reply messages ...
</code></pre>

Note the base class names don't have an underscore before the "base" portion of the name. This is intentional due to how we generate names; if a protocol had a message named "base", it would get mapped to the name @PROTO_request_base@, which would conflict with the automatically generated base class.

Back