2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var privUtils_ = xwalk.utils;
18 var validator_ = privUtils_.validator;
19 var types_ = validator_.Types;
20 var native_ = new xwalk.utils.NativeManager(extension);
22 var AbortError = new WebAPIException('AbortError', 'An unknown error occurred');
25 var MAX_TENSORS_INFO_COUNT = 16;
29 var TensorRawData = function() {
30 Object.defineProperties(this, {
34 throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
40 throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
46 throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
68 var _ValidTensorsDataIds = new Set();
70 function _CheckIfTensorsDataNotDisposed(id) {
71 if (false == _ValidTensorsDataIds.has(id)) {
72 throw new WebAPIException(WebAPIException.ABORT_ERR, 'TensorsData is disposed');
76 var TensorsData = function(id, tensorsInfoId) {
77 Object.defineProperties(this, {
81 return this._tensorsInfo.count;
87 _CheckIfTensorsDataNotDisposed(this._id);
88 return this._tensorsInfo.clone();
91 _id: { value: id, writable: false, enumerable: false },
93 value: new TensorsInfo(tensorsInfoId),
98 _ValidTensorsDataIds.add(id);
101 TensorsData.prototype.getTensorRawData = function() {
102 _CheckIfTensorsDataNotDisposed();
103 throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
106 TensorsData.prototype.setTensorData = function() {
107 _CheckIfTensorsDataNotDisposed();
108 throw new WebAPIException(WebAPIException.ABORT_ERR, 'Not implemented');
111 TensorsData.prototype.dispose = function() {
112 if (false == _ValidTensorsDataIds.has(this._id)) {
113 privUtils_.log('TensorsData already disposed');
117 tensorsDataId: this._id
120 var result = native_.callSync('MLTensorsDataDispose', callArgs);
122 if (native_.isFailure(result)) {
125 _ValidTensorsDataIds['delete'](this._id);
126 // underlying tensorsInfo_ is also invalid
127 _ValidTensorsInfoIds['delete'](this._tensorsInfo._id);
132 function tensorsInfoCountGetter(id) {
133 var result = native_.callSync('MLTensorsInfoCountGetter', { tensorsInfoId: id });
135 if (native_.isFailure(result)) {
138 return native_.getResultObject(result);
142 var _ValidTensorsInfoIds = new Set();
144 function _CheckIfTensorsInfoNotDisposed(id) {
145 if (false == _ValidTensorsInfoIds.has(id)) {
146 throw new WebAPIException(WebAPIException.ABORT_ERR, 'TensorsInfo is disposed');
150 var TensorsInfo = function() {
151 validator_.isConstructorCall(this, TensorsInfo);
154 if (arguments.length == 0) {
155 var result = native_.callSync('MLTensorsInfoCreate');
156 if (native_.isFailure(result)) {
161 var args = validator_.validateArgs(arguments, [
170 _ValidTensorsInfoIds.add(id);
172 Object.defineProperties(this, {
176 return tensorsInfoCountGetter(this._id);
179 _id: { value: id, writable: false, enumerable: false }
183 var TensorsInfoAddTensorInfoValidExceptions = [
184 'InvalidValuesError',
190 TensorsInfo.prototype.addTensorInfo = function() {
191 _CheckIfTensorsInfoNotDisposed(this._id);
192 var args = validator_.validateArgs(arguments, [
202 values: Object.values(TensorType),
212 args.dimensions.forEach(function(d) {
213 if (Number.isInteger(d) == false) {
214 throw new WebAPIException(
215 WebAPIException.TYPE_MISMATCH_ERR,
216 'dimensions array has to contain only integers'
224 dimensions: args.dimensions,
225 tensorsInfoId: this._id
228 var result = native_.callSync('MLTensorsInfoAddTensorInfo', callArgs);
229 if (native_.isFailure(result)) {
230 throw native_.getErrorObjectAndValidate(
232 TensorsInfoAddTensorInfoValidExceptions,
238 var TensorsInfoGettersSettersValidExceptions = [
240 'InvalidValuesError',
244 TensorsInfo.prototype.clone = function() {
245 _CheckIfTensorsInfoNotDisposed(this._id);
247 tensorsInfoId: this._id
250 var result = native_.callSync('MLTensorsInfoClone', callArgs);
252 if (native_.isFailure(result)) {
256 return new TensorsInfo(result.id);
259 TensorsInfo.prototype.equals = function() {
260 _CheckIfTensorsInfoNotDisposed(this._id);
261 var args = validator_.validateArgs(arguments, [
264 type: types_.PLATFORM_OBJECT,
269 if (this._id == args.other._id) {
274 tensorsInfoId: this._id,
275 otherId: args.other._id
278 var result = native_.callSync('MLTensorsInfoEquals', callArgs);
280 if (native_.isFailure(result)) {
284 return native_.getResultObject(result);
287 TensorsInfo.prototype.getDimensions = function() {
288 _CheckIfTensorsInfoNotDisposed(this._id);
289 var args = validator_.validateArgs(arguments, [
296 if (!args.has.index) {
297 throw new WebAPIException(
298 WebAPIException.INVALID_VALUES_ERR,
299 'Invalid parameter: index is undefined'
305 tensorsInfoId: this._id
308 var result = native_.callSync('MLTensorsInfoGetDimensions', callArgs);
310 if (native_.isFailure(result)) {
311 throw native_.getErrorObjectAndValidate(
313 TensorsInfoGettersSettersValidExceptions,
317 return native_.getResultObject(result);
320 var TensorsInfoSetDimensionsExceptions = [
321 'InvalidValuesError',
327 TensorsInfo.prototype.setDimensions = function() {
328 _CheckIfTensorsInfoNotDisposed(this._id);
329 var args = validator_.validateArgs(arguments, [
340 args.dimensions.forEach(function(d) {
341 if (Number.isInteger(d) == false) {
342 throw new WebAPIException(
343 WebAPIException.TYPE_MISMATCH_ERR,
344 'dimensions array has to contain only integers'
351 dimensions: args.dimensions,
352 tensorsInfoId: this._id
355 var result = native_.callSync('MLTensorsInfoSetDimensions', callArgs);
357 if (native_.isFailure(result)) {
358 throw native_.getErrorObjectAndValidate(
360 TensorsInfoSetDimensionsExceptions,
366 TensorsInfo.prototype.getTensorName = function() {
367 _CheckIfTensorsInfoNotDisposed(this._id);
368 var args = validator_.validateArgs(arguments, [
375 if (!args.has.index) {
376 throw new WebAPIException(
377 WebAPIException.INVALID_VALUES_ERR,
378 'Invalid parameter: index is undefined'
384 tensorsInfoId: this._id
387 var result = native_.callSync('MLTensorsInfoGetTensorName', callArgs);
389 if (native_.isFailure(result)) {
390 throw native_.getErrorObjectAndValidate(
392 TensorsInfoGettersSettersValidExceptions,
396 return native_.getResultObject(result);
399 TensorsInfo.prototype.setTensorName = function() {
400 _CheckIfTensorsInfoNotDisposed(this._id);
401 var args = validator_.validateArgs(arguments, [
412 if (!args.has.index) {
413 throw new WebAPIException(
414 WebAPIException.INVALID_VALUES_ERR,
415 'Invalid parameter: index is undefined'
419 if (!args.has.name) {
420 throw new WebAPIException(
421 WebAPIException.INVALID_VALUES_ERR,
422 'Invalid parameter: name is undefined'
429 tensorsInfoId: this._id
432 var result = native_.callSync('MLTensorsInfoSetTensorName', callArgs);
434 if (native_.isFailure(result)) {
435 throw native_.getErrorObjectAndValidate(
437 TensorsInfoGettersSettersValidExceptions,
443 TensorsInfo.prototype.getTensorType = function() {
444 _CheckIfTensorsInfoNotDisposed(this._id);
445 var args = validator_.validateArgs(arguments, [
452 if (!args.has.index) {
453 throw new WebAPIException(
454 WebAPIException.INVALID_VALUES_ERR,
455 'Invalid parameter: index is undefined'
461 tensorsInfoId: this._id
464 var result = native_.callSync('MLTensorsInfoGetTensorType', callArgs);
466 if (native_.isFailure(result)) {
467 throw native_.getErrorObjectAndValidate(
469 TensorsInfoGettersSettersValidExceptions,
473 return native_.getResultObject(result);
476 var TensorsInfoSetTensorTypeValidExceptions = [
478 'InvalidValuesError',
483 TensorsInfo.prototype.setTensorType = function() {
484 _CheckIfTensorsInfoNotDisposed(this._id);
485 var args = validator_.validateArgs(arguments, [
493 values: Object.values(TensorType)
500 tensorsInfoId: this._id
503 var result = native_.callSync('MLTensorsInfoSetTensorType', callArgs);
505 if (native_.isFailure(result)) {
506 throw native_.getErrorObjectAndValidate(
508 TensorsInfoSetTensorTypeValidExceptions,
514 TensorsInfo.prototype.getTensorSize = function() {
515 _CheckIfTensorsInfoNotDisposed(this._id);
516 var args = validator_.validateArgs(arguments, [
523 if (!args.has.index) {
524 throw new WebAPIException(
525 WebAPIException.INVALID_VALUES_ERR,
526 'Invalid parameter: index is undefined'
532 tensorsInfoId: this._id
535 var result = native_.callSync('MLTensorsInfoGetTensorSize', callArgs);
537 if (native_.isFailure(result)) {
538 throw native_.getErrorObjectAndValidate(
540 TensorsInfoGettersSettersValidExceptions,
544 return native_.getResultObject(result);
547 var TensorsInfoGetTensorsDataValidExceptions = ['AbortError', 'NotSupportedError'];
549 TensorsInfo.prototype.getTensorsData = function() {
550 _CheckIfTensorsInfoNotDisposed(this._id);
552 tensorsInfoId: this._id
555 var result = native_.callSync('MLTensorsInfoGetTensorsData', callArgs);
557 if (native_.isFailure(result)) {
558 throw native_.getErrorObjectAndValidate(
560 TensorsInfoGetTensorsDataValidExceptions,
564 return new TensorsData(result.tensorsDataId, result.tensorsInfoId);
567 TensorsInfo.prototype.dispose = function() {
568 if (false == _ValidTensorsInfoIds.has(this._id)) {
572 tensorsInfoId: this._id
575 var result = native_.callSync('MLTensorsInfoDispose', callArgs);
577 if (native_.isFailure(result)) {
580 _ValidTensorsInfoIds['delete'](this._id);