@@ -31,7 +31,7 @@ if (it.spec.fields.length === 0) {
3131 },
3232 " name" : " _dummy"
3333 });
34- } /* if */
34+ }
3535
3636function getPrimitiveNameByType(type) {
3737 if (type.type === 'bool') {
@@ -108,7 +108,6 @@ function getTypedArrayName(type) {
108108 default:
109109 typedArrayName = '';
110110 }
111-
112111 return typedArrayName;
113112}
114113
@@ -267,80 +266,84 @@ const {{=refObjectArrayType}} = StructType({
267266 capacity: ref.types.size_t
268267});
269268
270- // Define the wrapper class.
269+ {{ /* Define the wrapper class for the message. */ }}
271270class {{=objectWrapper}} {
272271 constructor(other, willCheckConsistency = false) {
273- this._wrapperFields = {};
274- this._willCheckConsistency = willCheckConsistency;
275- {{~ it.spec.fields :field}}
276- {{? field.type.isArray && field.type.isPrimitiveType && !isTypedArrayType(field.type)}}
277- this._{{=field.name}}Array = [];
278- {{?}}
279- {{~}}
272+ this._initMembers(willCheckConsistency);
273+ this._setDefaults();
274+ {{/* Construct `this` from `other`. */ }}
280275
281276 if (typeof other === 'object' && other._refObject) {
277+ // this._initMembers(willCheckConsistency);
282278 this._refObject = new {{=refObjectType}}(other._refObject.toObject());
283279 {{~ it.spec.fields :field}}
284- {{? field.type.isPrimitiveType && !field.type.isArray}}
285- this._{{=field.name}}Intialized = true;
286- {{?}}
287-
288- {{? field.type.isArray}}
289- this._wrapperFields.{{=field.name}} = {{=getWrapperNameByType(field.type)}}.createArray();
290- this._wrapperFields.{{=field.name}}.copy(other._wrapperFields.{{=field.name}});
291- {{? field.type.isPrimitiveType && !isTypedArrayType(field.type)}}
292- this.{{=field.name}} = other.{{=field.name}};
293- {{?}}
294- {{?? !field.type.isPrimitiveType || (field.type.type === 'string' && it.spec.msgName !== 'String')}}
295- this._wrapperFields.{{=field.name}} = new {{=getWrapperNameByType(field.type)}}(other._wrapperFields.{{=field.name}});
296- {{?}}
280+ {{? field.type.isPrimitiveType && !field.type.isArray}}
281+ this._{{=field.name}}Intialized = true;
282+ {{?}}
283+
284+ {{? field.type.isArray}}
285+ this._wrapperFields.{{=field.name}} = {{=getWrapperNameByType(field.type)}}.createArray();
286+ this._wrapperFields.{{=field.name}}.copy(other._wrapperFields.{{=field.name}});
287+ {{? field.type.isPrimitiveType && !isTypedArrayType(field.type)}}
288+ this.{{=field.name}} = other.{{=field.name}};
289+ {{?}}
290+ {{?? !field.type.isPrimitiveType || (field.type.type === 'string' && it.spec.msgName !== 'String')}}
291+ this._wrapperFields.{{=field.name}} = new {{=getWrapperNameByType(field.type)}}(other._wrapperFields.{{=field.name}});
292+ {{?}}
297293 {{~}}
298294 } else if (typeof other !== 'undefined') {
299- this._initMembers();
295+ {{/* Try to construct the message from a plan object of JavaScript. */ }}
296+ // this._initMembers(willCheckConsistency);
300297 translator.constructFromPlanObject(this, other);
301298 } else {
302- this._initMembers();
299+ // this._initMembers(willCheckConsistency );
303300 }
304301 this.freeze();
305302 }
306303
307- _initMembers() {
308- this._refObject = new {{=refObjectType}}();
304+ _setDefaults() {
309305 {{~ it.spec.fields :field}}
310- {{? it.spec.isEmpty}}
311- this._{{=field.name}}Intialized = true;
312- {{??}}
313- {{? field.type.isPrimitiveType && !field.type.isArray}}
314- {{? field.default_value === null}}
315- this._{{=field.name}}Intialized = false;
316- {{?? field.type.type === 'string' || field.type.type === 'wstring'}}
317- this._refObject.{{=field.name}} = " {{=field.default_value.replace(/" /g, '\\" ')}}" ;
318- this._{{=field.name}}Intialized = true;
319- {{??}}
320- this._refObject.{{=field.name}} = {{=field.default_value}};
321- this._{{=field.name}}Intialized = true;
322- {{?}}
323- {{?}}
306+ {{/* Set the flag of initialization. */ }}
307+ {{? it.spec.isEmpty}}
308+ this._{{=field.name}}Intialized = true;
309+ {{?? field.type.isPrimitiveType && !field.type.isArray && field.default_value}}
310+ this._{{=field.name}}Intialized = true;
311+ {{? field.type.type === 'string' || field.type.type === 'wstring'}}
312+ this._refObject.{{=field.name}} = " {{=field.default_value.replace(/" /g, '\\" ')}}" ;
313+ {{??}}
314+ this._refObject.{{=field.name}} = {{=field.default_value}};
315+ {{?}}
316+ {{?? field.type.isPrimitiveType && !isTypedArrayType(field.type) && field.default_value}}
317+ this._{{=field.name}}Array = {{=JSON.stringify(field.default_value)}};
318+ {{?? field.type.isPrimitiveType && isTypedArrayType(field.type) && field.default_value}}
319+ this._wrapperFields.{{=field.name}}.fill({{=getTypedArrayName(field.type)}}.from({{=JSON.stringify(field.default_value)}}));
320+ {{??}}
321+ this._{{=field.name}}Intialized = false;
322+ {{?}}
323+ {{~}}
324+ }
324325
325- {{? field.type.isArray}}
326- this._wrapperFields.{{=field.name}} = {{=getWrapperNameByType(field.type)}}.createArray();
327- {{? field.default_value !== null && field.type.isPrimitiveType && !isTypedArrayType(field.type)}}
328- this._{{=field.name}}Array = {{=JSON.stringify(field.default_value)}};
329- {{?}}
330- {{? field.default_value !== null && field.type.isPrimitiveType && isTypedArrayType(field.type)}}
331- this._wrapperFields.{{=field.name}}.fill({{=getTypedArrayName(field.type)}}.from({{=JSON.stringify(field.default_value)}}));
332- {{?}}
333- {{? field.type.type === 'string' && field.type.isFixedSizeArray}}
334- for (let i = 0; i < {{=field.type.arraySize}}; i++) {
335- primitiveTypes.initString(this._refObject.{{=field.name}}[i]);
336- }
337- {{?}}
338- {{?? !field.type.isPrimitiveType || (field.type.type === 'string' && it.spec.msgName !== 'String')}}
339- this._wrapperFields.{{=field.name}} = new {{=getWrapperNameByType(field.type)}}();
340- {{?? it.spec.msgName === 'String'}}
341- primitiveTypes.initString(this._refObject);
342- {{?}}
343- {{?}}
326+ _initMembers(willCheckConsistency) {
327+ this._willCheckConsistency = willCheckConsistency;
328+ this._wrapperFields = {};
329+ this._refObject = new {{=refObjectType}}();
330+
331+ {{~ it.spec.fields :field}}
332+ {{? field.type.isArray}}
333+ this._wrapperFields.{{=field.name}} = {{=getWrapperNameByType(field.type)}}.createArray();
334+ {{? field.type.type === 'string' && field.type.isFixedSizeArray}}
335+ for (let i = 0; i < {{=field.type.arraySize}}; i++) {
336+ primitiveTypes.initString(this._refObject.{{=field.name}}[i]);
337+ }
338+ {{?}}
339+ {{? field.type.isArray && field.type.isPrimitiveType && !isTypedArrayType(field.type)}}
340+ this._{{=field.name}}Array = [];
341+ {{?}}
342+ {{?? !field.type.isPrimitiveType || (field.type.type === 'string' && it.spec.msgName !== 'String')}}
343+ this._wrapperFields.{{=field.name}} = new {{=getWrapperNameByType(field.type)}}();
344+ {{?? it.spec.msgName === 'String'}}
345+ primitiveTypes.initString(this._refObject);
346+ {{?}}
344347 {{~}}
345348 }
346349
@@ -371,6 +374,7 @@ class {{=objectWrapper}} {
371374 return this._refObject.ref();
372375 }
373376
377+ {{ /* Allocate the memory following the struct of the message*/ }}
374378 freeze(own = false, checkConsistency = false) {
375379 {{~ it.spec.fields :field}}
376380 {{? field.type.isPrimitiveType && !field.type.isArray}}
@@ -395,7 +399,7 @@ class {{=objectWrapper}} {
395399 }
396400 }
397401 }
398- // For non-typed array like int64/uint64/bool.
402+ {{ /* For non-typed array like int64/uint64/bool. */ }}
399403 {{?? true}}
400404 this._refObject.{{=field.name}} = this._{{=field.name}}Array;
401405 {{?}}
@@ -459,7 +463,7 @@ class {{=objectWrapper}} {
459463 for (let index = 0; index < {{=field.type.arraySize}}; index++) {
460464 this._{{=field.name}}Array[index] = refObject.{{=field.name}}[index].data;
461465 }
462- // For non-typed array like int64/uint64/bool.
466+ {{ /* For non-typed array like int64/uint64/bool. */ }}
463467 {{?? true}}
464468 this._{{=field.name}}Array = refObject.{{=field.name}}.toArray();
465469 {{?}}
@@ -494,7 +498,7 @@ class {{=objectWrapper}} {
494498 if (refObject.{{=field.name}}.size != 0) {
495499 {{=getWrapperNameByType(field.type)}}.ArrayType.freeArray(refObject.{{=field.name}});
496500 if ({{=getWrapperNameByType(field.type)}}.ArrayType.useTypedArray) {
497- // Do nothing, the v8 will take the ownership of the ArrayBuffer used by the typed array.
501+ {{ /* Do nothing, the v8 will take the ownership of the ArrayBuffer used by the typed array. */ }}
498502 } else {
499503 deallocator.freeStructMember(refObject.{{=field.name}}, {{=getWrapperNameByType(field.type)}}.refObjectArrayType, 'data');
500504 }
@@ -657,7 +661,7 @@ class {{=objectWrapper}} {
657661 }
658662}
659663
660- // Define the wrapper of array class.
664+ {{ /* Define the wrapper class for the message array. */ }}
661665class {{=arrayWrapper}} {
662666 constructor(size = 0) {
663667 this._resize(size );
@@ -670,13 +674,13 @@ class {{=arrayWrapper}} {
670674 fill(values) {
671675 {{? usePlainTypedArray}}
672676 if (Array.isArray(values)) {
673- // Convert JavaScript array
677+ {{ /* Convert JavaScript array. */ }}
674678 this._wrappers = new {{=currentTypedArray}}(values);
675679 } else {
676680 this._wrappers = values;
677681 }
678682 {{?? isPrimitivePackage(it.spec.baseType)}}
679- // Now for primitive arrays, only string/bool/int64/uint64 array drops here.
683+ {{ /* For primitive arrays, only string/bool/int64/uint64 array drops here. */ }}
680684 const length = values.length;
681685 this._resize(length);
682686 for (let i = 0; i < length; ++i) {
@@ -697,10 +701,10 @@ class {{=arrayWrapper}} {
697701 {{?}}
698702 }
699703
700- // Put all data currently stored in `this._wrappers` into `this._refObject`
704+ {{ /* Put all data currently stored in `this._wrappers` into `this._refObject`. */ }}
701705 freeze(own) {
702706 {{? usePlainTypedArray}}
703- // When it's a TypedArray: no need to copy to `this._refArray`
707+ {{ /* When it's a TypedArray: no need to copy to `this._refArray`. */ }}
704708 {{?? true}}
705709 this._wrappers.forEach((wrapper, index) => {
706710 wrapper.freeze(own);
@@ -783,13 +787,13 @@ class {{=arrayWrapper}} {
783787 {{?}}
784788 }
785789
786- // Copy all data from `this._refObject` into `this._wrappers`
790+ {{ /* Copy all data from `this._refObject` into `this._wrappers`. */ }}
787791 copyRefObject(refObject) {
788792 this._refObject = refObject;
789793
790794 {{? usePlainTypedArray}}
791795 const byteLen = refObject.size * ref.types.{{=currentTypedArrayElementType}}.size ;
792- // An ArrayBuffer object that doesn't hold the ownership of the address
796+ {{ /* An ArrayBuffer object doesn't hold the ownership of memory block starting from address. */ }}
793797 const arrayBuffer = refObject.data.length !== 0 ?
794798 rclnodejs.createArrayBufferFromAddress(refObject.data.hexAddress(), byteLen) :
795799 Buffer.alloc(0);
@@ -814,7 +818,7 @@ class {{=arrayWrapper}} {
814818 {{? usePlainTypedArray}}
815819 this._wrappers = other._wrappers.slice();
816820 {{?? true}}
817- // Array deep copy
821+ {{ /* Array deep copy. */ }}
818822 other._wrappers.forEach((wrapper, index) => {
819823 this._wrappers[index].copy(wrapper);
820824 });
@@ -823,7 +827,7 @@ class {{=arrayWrapper}} {
823827
824828 static freeArray(refObject) {
825829 {{? usePlainTypedArray}}
826- // For TypedArray: .data will be 'free()'-ed in parent struct
830+ {{ /* For TypedArray, ` .data` will be 'free()'-ed in parent struct. */ }}
827831 {{?? true}}
828832 let refObjectArray = refObject.data;
829833 refObjectArray.length = refObject.size ;
@@ -851,7 +855,7 @@ class {{=arrayWrapper}} {
851855}
852856
853857{{? it.spec.constants != undefined && it.spec.constants.length}}
854- // Define constants ({{=it.spec.constants.length}} in total)
858+ {{ /* Define constants ({{=it.spec.constants.length}} in total). */ }}
855859{{~ it.spec.constants :c}}
856860{{? c.type === " string" }}
857861Object.defineProperty({{=objectWrapper}}, " {{=c.name}}" , {value: " {{=c.value}}" , writable: false, enumerable: true, configurable: true});
0 commit comments