зеркало из https://github.com/microsoft/napajs.git
Fix load() implementation of transportable object (#216)
* Fix load() implementation of transportable object * Add a test case for AutoTransport
This commit is contained in:
Родитель
a8d23e0d37
Коммит
9c9d8f611a
|
@ -1,12 +1,13 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
export {
|
||||
Transportable,
|
||||
TransportableObject,
|
||||
TransportContext,
|
||||
isTransportable,
|
||||
cid
|
||||
export {
|
||||
Transportable,
|
||||
TransportableObject,
|
||||
TransportContext,
|
||||
AutoTransportable,
|
||||
isTransportable,
|
||||
cid
|
||||
} from './transport/transportable';
|
||||
|
||||
export * from './transport/transport';
|
||||
|
|
|
@ -115,7 +115,10 @@ export class AutoTransportable extends TransportableObject {
|
|||
/// <param name='payload'> Payload to read from, which already have inner objects transported. </param>
|
||||
/// <param name='context'> Transport context for loading shared pointers, only usable for C++ addons that extends napa::module::ShareableWrap. </param>
|
||||
load(payload: object, context: TransportContext) {
|
||||
// Members have already been unmarshalled. Do nothing.
|
||||
// Members have already been unmarshalled.
|
||||
for (let property of Object.getOwnPropertyNames(payload)) {
|
||||
(<any>(this))[property] = (<any>(payload))[property];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
// Licensed under the MIT license.
|
||||
|
||||
import * as assert from 'assert';
|
||||
import * as path from "path";
|
||||
import * as path from 'path';
|
||||
import * as util from 'util';
|
||||
import * as napa from '../../lib/index';
|
||||
|
||||
export function bar(input: any) {
|
||||
|
@ -229,17 +230,30 @@ export class CanPass extends napa.transport.TransportableObject {
|
|||
}
|
||||
|
||||
load(payload: any, tc: napa.transport.TransportContext): void {
|
||||
// Do nothing, as all members are transportable, which will be restored automatically.
|
||||
// member '_allocator' is already unmarshalled.
|
||||
this._allocator = payload['_allocator'];
|
||||
}
|
||||
|
||||
_allocator: napa.memory.Allocator;
|
||||
}
|
||||
|
||||
@napa.transport.cid()
|
||||
export class CanAutoPass extends napa.transport.AutoTransportable {
|
||||
constructor(data: any) {
|
||||
super();
|
||||
this._data = data;
|
||||
}
|
||||
|
||||
_data: any;
|
||||
}
|
||||
|
||||
function testMarshallUnmarshall(input: any) {
|
||||
let tc = napa.transport.createTransportContext();
|
||||
let payload = napa.transport.marshall(input, tc);
|
||||
let expected = napa.transport.unmarshall(payload, tc);
|
||||
assert.equal(input.toString(), expected.toString());
|
||||
assert.equal(JSON.stringify(input), JSON.stringify(expected));
|
||||
assert.equal(util.inspect(input), util.inspect(expected));
|
||||
}
|
||||
|
||||
export function simpleTypeTransportTest() {
|
||||
|
@ -256,6 +270,10 @@ export function jsTransportTest() {
|
|||
testMarshallUnmarshall(new CanPass(napa.memory.crtAllocator));
|
||||
}
|
||||
|
||||
export function jsAutoTransportTest() {
|
||||
testMarshallUnmarshall(new CanAutoPass({ a: 'foo', b: 'bar', c: 123 }));
|
||||
}
|
||||
|
||||
export function functionTransportTest() {
|
||||
testMarshallUnmarshall(() => { return 0; });
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче