All files / src/Ice Operation.js

98.38% Statements 550/559
95.65% Branches 132/138
100% Functions 24/24
98.38% Lines 550/559

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 56041x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 1422x 1422x 1422x 1422x 381x 1419x 197x 197x 197x 129x 129x 197x 1422x 1422x 1422x 1422x 1422x 1422x 1422x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 722x 722x 722x 722x 722x 722x 722x 722x 722x 722x 489x 489x 489x 722x 722x 722x 722x 722x 431x 610x 610x 610x 610x 101x 101x 610x 431x 722x 722x 722x 722x 722x 722x 722x 282x 282x 323x 323x 323x 323x 89x 89x 323x 282x 722x 96x 96x 722x 722x 722x 722x 722x 722x 65x 76x 76x 65x 722x 722x 722x 722x 722x 722x 722x 41x 41x 41x 1644x 1644x 1644x 41x 41x 646x 646x 646x 646x 646x 341x 341x 341x 341x 341x 341x 646x 646x 41x 41x 1602x 1602x 2137x 357x   357x 357x 357x 2137x 141x 139x 141x 1780x 1639x 1639x 1602x 1602x 1602x 1602x 1602x 1602x 1243x 973x 973x 1243x 1602x 1602x 1602x 1602x 1602x 807x 807x 1602x 1602x 1602x 1602x 1602x 357x 357x 1602x 1602x 136x 136x 1602x 41x 1407x 1407x 1407x 1407x 1407x 1354x 1354x 1092x 1092x 1354x 1407x 1407x 1407x 1407x 1407x 520x 520x 1407x 1407x 1407x 1407x 1407x 436x 436x 436x 1407x 1407x 118x 118x 1407x 41x 41x 833x 833x         833x 41x 901x 901x 901x 901x 901x 901x     901x 901x 833x 833x 901x 901x 901x 901x 901x 901x 197x 901x 704x 704x 704x 704x 899x 899x 899x 899x 836x 836x   836x 303x 303x 836x 836x 141x   141x 141x 141x 836x 695x 695x 520x 520x 695x 695x 695x 695x 695x 695x 899x 899x 899x 901x 3x 901x 833x 833x 901x 41x 872x 872x 872x 186x 186x 186x 181x 175x 175x 181x 181x 181x 181x 186x 872x 872x 41x 41x 41x 903x 903x 903x 903x 903x 903x 903x 903x 903x 903x 903x 903x 903x 903x 348x 348x 348x 348x 348x 348x 348x 1377x 691x 691x 1377x 1377x 348x 348x 5x 5x 5x 5x 5x 348x 903x 903x 343x 343x 343x 343x 343x 343x 343x 343x 343x 343x 158x 158x 158x 158x 158x 446x 158x 14x 14x 158x 446x 446x 158x 158x 158x 158x 158x 145x 145x 145x 142x 142x 145x 145x 158x 343x 343x 341x 901x 901x 901x 3x 3x 833x 899x 65x 65x 65x 65x 341x 341x 341x 341x 341x 341x 341x 341x 341x 341x 341x 156x 156x 341x 343x 903x 903x 903x 41x 7778x 7778x 7778x 7778x 7778x 7778x 3457x 3457x 3457x 3457x 381x 381x 3457x 3457x 3457x 3457x 3457x 734x 732x 732x 732x 732x 919x 919x 919x 860x 151x 20x 20x 20x 20x 151x 860x 919x 712x 712x 734x 734x 3457x 3457x 3457x 967x 898x 898x 898x 898x 898x 898x 898x 898x 898x 898x 807x 807x 898x 898x 898x 967x 967x 3457x 3457x 3457x 3457x 3457x 3457x 3457x 3457x 3457x 3457x 3457x 7778x 7778x 41x 41x 1653x 1644x 1644x 1653x 1653x 903x 903x 903x 903x 903x 903x 2x 2x 901x 901x 1653x 1653x 1653x 30x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 5x 1653x 1653x 1653x 1653x 1644x 7778x 7778x 1644x 1653x 1653x 1653x 1653x 1653x 1407x 204x 204x 11201x 793x 793x 11201x 204x 1407x 1653x 1653x 1653x 1653x 1653x 1653x  
// Copyright (c) ZeroC, Inc.
 
import { Ice as Ice_OperationMode } from "./OperationMode.js";
const { OperationMode } = Ice_OperationMode;
import { FormatType } from "./FormatType.js";
import { MarshalException, OperationNotExistException } from "./LocalExceptions.js";
import { ObjectPrx } from "./ObjectPrx.js";
import { TypeRegistry } from "./TypeRegistry.js";
 
import {
    ByteHelper,
    BoolHelper,
    ShortHelper,
    IntHelper,
    LongHelper,
    FloatHelper,
    DoubleHelper,
    StringHelper,
} from "./StreamHelpers.js";
import { Value } from "./Value.js";
 
const builtinHelpers = [
    ByteHelper,
    BoolHelper,
    ShortHelper,
    IntHelper,
    LongHelper,
    FloatHelper,
    DoubleHelper,
    StringHelper,
    ObjectPrx,
    Value,
];
 
function parseParam(p) {
    let type = p[0];
    const t = typeof type;
    if (t === "number") {
        type = builtinHelpers[p[0]];
    } else if (t === "string") {
        const typeName = p[0];
        type = TypeRegistry.getProxyType(typeName);
        if (type === undefined) {
            type = TypeRegistry.getValueType(typeName);
        }
    }
 
    return {
        type: type,
        isObject: p[1] === true,
        tag: p[2], // Optional tag, which may not be present - an undefined tag means "not optional".
    };
}
 
//
// Each operation descriptor is a property. The key is the "on-the-wire"
// name, and the value is an array consisting of the following elements:
//
//  0: native method name in case of a keyword conflict (e.g., "_while"),
//     otherwise an empty string
//  1: mode (undefined == Normal or int)
//  2: format (undefined == Default or int)
//  3: return type (undefined if void, or [type, tag])
//  4: in params (undefined if none, or array of [type, tag])
//  5: out params (undefined if none, or array of [type, tag])
//  6: exceptions (undefined if none, or array of types)
//  7: sends classes (true or undefined)
//  8: returns classes (true or undefined)
//
function parseOperation(name, arr) {
    const r = {};
 
    r.name = name;
    r.servantMethod = arr[0] ? arr[0] : name;
    r.mode = OperationMode.valueOf(arr[1]);
    r.format = arr[2] ? FormatType.valueOf(arr[2]) : null;
 
    let ret;
    if (arr[3]) {
        ret = parseParam(arr[3]);
        ret.pos = 0;
    }
    r.returns = ret;
 
    const inParams = [];
    const inParamsOpt = [];
    if (arr[4]) {
        for (let i = 0; i < arr[4].length; ++i) {
            const p = parseParam(arr[4][i]);
            p.pos = i;
            inParams.push(p);
            if (p.tag) {
                inParamsOpt.push(p);
            }
        }
    }
    inParamsOpt.sort((p1, p2) => p1.tag - p2.tag); // Sort by tag.
    r.inParams = inParams;
    r.inParamsOpt = inParamsOpt;
 
    const outParams = [];
    const outParamsOpt = [];
    if (arr[5]) {
        const offs = ret ? 1 : 0;
        for (let i = 0; i < arr[5].length; ++i) {
            const p = parseParam(arr[5][i]);
            p.pos = i + offs;
            outParams.push(p);
            if (p.tag) {
                outParamsOpt.push(p);
            }
        }
    }
    if (ret && ret.tag) {
        outParamsOpt.push(ret);
    }
    outParamsOpt.sort((p1, p2) => p1.tag - p2.tag); // Sort by tag.
    r.outParams = outParams;
    r.outParamsOpt = outParamsOpt;
 
    const exceptions = [];
    if (arr[6]) {
        for (let i = 0; i < arr[6].length; ++i) {
            exceptions.push(arr[6][i]);
        }
    }
    r.exceptions = exceptions;
 
    r.sendsClasses = arr[7] === true;
    r.returnsClasses = arr[8] === true;
 
    return r;
}
 
class OpTable {
    constructor(ops) {
        this.raw = ops;
        this.parsed = {};
    }
 
    find(name) {
        //
        // Check if we've already parsed the operation.
        //
        let op = this.parsed[name];
        if (op === undefined && this.raw[name] !== undefined) {
            //
            // We haven't parsed it yet, but we found a match for the name, so parse it now.
            //
            op = parseOperation(name, this.raw[name]);
            this.parsed[name] = op;
        }
        return op;
    }
}
 
function unmarshalParams(is, retvalInfo, allParamInfo, optParamInfo, usesClasses, params) {
    const readParam = (p, optional) => {
        if (optional) {
            if (p.isObject) {
                throw new MarshalException("cannot unmarshal an optional class");
            } else {
                params[p.pos] = p.type.readOptional(is, p.tag);
            }
        } else if (p.isObject) {
            is.readValue(obj => {
                params[p.pos] = obj;
            }, p.type);
        } else {
            params[p.pos] = p.type.read(is);
        }
    };
 
    //
    // First read all required params.
    //
    for (let i = 0; i < allParamInfo.length; ++i) {
        if (!allParamInfo[i].tag) {
            readParam(allParamInfo[i], false);
        }
    }
 
    //
    // Then read a required return value (if any).
    //
    if (retvalInfo) {
        readParam(retvalInfo, false);
    }
 
    //
    // Then read all optional params.
    //
    for (let i = 0; i < optParamInfo.length; ++i) {
        readParam(optParamInfo[i], true);
    }
 
    if (usesClasses) {
        is.readPendingValues();
    }
}
 
function marshalParams(os, params, retvalInfo, paramInfo, optParamInfo, usesClasses) {
    //
    // Write the required params.
    //
    for (let i = 0; i < paramInfo.length; ++i) {
        const p = paramInfo[i];
        if (!p.tag) {
            p.type.write(os, params[p.pos]);
        }
    }
 
    //
    // retvalInfo should only be provided if there is a non-void required return value.
    //
    if (retvalInfo) {
        retvalInfo.type.write(os, params[retvalInfo.pos]);
    }
 
    //
    // Write the optional params.
    //
    for (let i = 0; i < optParamInfo.length; ++i) {
        const p = optParamInfo[i];
        p.type.writeOptional(os, p.tag, params[p.pos]);
    }
 
    if (usesClasses) {
        os.writePendingValues();
    }
}
 
// Ensures the operation mode of an incoming request is not idempotent.
function checkNonIdempotent(current) {
    if (current.mode !== OperationMode.Normal) {
        throw new MarshalException(
            `Operation mode mismatch for operation '${current.operation}': received ${current.node} for non-idempotent operation`,
        );
    }
}
 
function dispatchImpl(servant, op, request) {
    //
    // Check to make sure the servant implements the operation.
    //
    const method = servant[op.servantMethod];
    if (typeof method !== "function") {
        throw new OperationNotExistException();
    }
 
    if (op.mode === OperationMode.Normal) {
        checkNonIdempotent(request.current);
    }
 
    //
    // Unmarshal the in params (if any).
    //
    const params = [];
    if (op.inParams.length === 0) {
        request.inputStream.skipEmptyEncapsulation();
    } else {
        request.inputStream.startEncapsulation();
        unmarshalParams(request.inputStream, undefined, op.inParams, op.inParamsOpt, op.sendsClasses, params);
        request.inputStream.endEncapsulation();
    }
 
    params.push(request.current);
 
    const marshalFn = function (params) {
        const numExpectedResults = op.outParams.length + (op.returns ? 1 : 0);
        if (numExpectedResults > 1 && !(params instanceof Array)) {
            throw new MarshalException(`operation '${op.servantMethod}' should return an array`);
        } else if (numExpectedResults === 1) {
            params = [params]; // Wrap a single out parameter in an array.
        }
 
        if (op.returns === undefined && op.outParams.length === 0) {
            if (params && params.length > 0) {
                throw new MarshalException(`operation '${op.servantMethod}' shouldn't return any value`);
            } else {
                return request.current.createEmptyOutgoingResponse();
            }
        } else {
            let retvalInfo;
            if (op.returns && !op.returns.tag) {
                retvalInfo = op.returns;
            }
 
            return request.current.createOutgoingResponseWithResult(
                ostr => marshalParams(ostr, params, retvalInfo, op.outParams, op.outParamsOpt, op.returnsClasses),
                op.format,
            );
        }
    };
 
    const results = method.apply(servant, params);
    if (results instanceof Promise) {
        return results.then(marshalFn);
    } else {
        return marshalFn(results);
    }
}
 
function getServantMethodFromInterfaces(interfaces, methodName, all) {
    let method;
    for (let i = 0; method === undefined && i < interfaces.length; ++i) {
        const intf = interfaces[i];
        method = intf[methodName];
        if (method === undefined) {
            if (all.indexOf(intf) === -1) {
                all.push(intf);
            }
            if (intf._iceImplements) {
                method = getServantMethodFromInterfaces(intf._iceImplements, methodName, all);
            }
        }
    }
    return method;
}
 
const dispatchPrefix = "_iceD_";
 
function getServantMethod(servantType, name) {
    //
    // The dispatch method is named _iceD_<Slice name> and is stored in the type (not the prototype).
    //
    const methodName = dispatchPrefix + name;
 
    //
    // First check the servant type.
    //
    let method = servantType[methodName];
 
    let allInterfaces;
 
    if (method === undefined) {
        allInterfaces = [];
 
        //
        // Now check the prototypes of the implemented interfaces.
        //
        let curr = servantType;
        while (curr && method === undefined) {
            if (curr._iceImplements) {
                method = getServantMethodFromInterfaces(curr._iceImplements, methodName, allInterfaces);
            }
            curr = Object.getPrototypeOf(curr);
        }
 
        if (method !== undefined) {
            //
            // Add the method to the servant's type.
            //
            servantType[methodName] = method;
        }
    }
 
    if (method === undefined) {
        //
        // Next check the op table for the servant's type.
        //
        let op;
        if (servantType._iceOps) {
            op = servantType._iceOps.find(name);
        }
 
        let source;
        if (op === undefined) {
            //
            // Now check the op tables of the base types.
            //
            let parent = Object.getPrototypeOf(servantType);
            while (op === undefined && parent) {
                if (parent._iceOps) {
                    if ((op = parent._iceOps.find(name)) !== undefined) {
                        source = parent;
                    }
                }
                parent = Object.getPrototypeOf(parent);
            }
 
            //
            // Now check the op tables of all base interfaces.
            //
            for (let i = 0; op === undefined && i < allInterfaces.length; ++i) {
                const intf = allInterfaces[i];
                if (intf._iceOps) {
                    if ((op = intf._iceOps.find(name)) !== undefined) {
                        source = intf;
                    }
                }
            }
        }
 
        if (op !== undefined) {
            method = async function (servant, request) {
                try {
                    const result = dispatchImpl(servant, op, request);
                    if (result instanceof Promise) {
                        return await result;
                    }
                    return result;
                } catch (ex) {
                    const communicator = request.current.adapter.getCommunicator();
                    console.assert(communicator !== null);
                    return request.current.createOutgoingResponseWithException(ex, communicator);
                }
            };
 
            //
            // Add the method to the servant type.
            //
            servantType[methodName] = method;
 
            //
            // Also add the method to the type in which the operation was found.
            //
            if (source) {
                source[methodName] = method;
            }
        }
    }
 
    return method;
}
 
function addProxyOperation(proxyType, name, data) {
    const method = data[0] ? data[0] : name;
 
    let op = null;
 
    proxyType.prototype[method] = function (...args) {
        //
        // Parse the operation data on the first invocation of a proxy method.
        //
        if (op === null) {
            op = parseOperation(name, data);
        }
 
        const ctx = args[op.inParams.length]; // The request context is the last argument (if present).
 
        let marshalFn = null;
        if (op.inParams.length > 0) {
            marshalFn = function (os, params) {
                //
                // Validate the parameters.
                //
                for (let i = 0; i < op.inParams.length; ++i) {
                    const p = op.inParams[i];
                    const v = params[p.pos];
                    if (!p.tag || (v !== undefined && v !== null)) {
                        if (typeof p.type.validate === "function") {
                            if (!p.type.validate(v)) {
                                throw new MarshalException(
                                    `invalid value for argument ${i + 1} in operation '${op.servantMethod}'`,
                                );
                            }
                        }
                    }
                }
 
                marshalParams(os, params, undefined, op.inParams, op.inParamsOpt, op.sendsClasses);
            };
        }
 
        let unmarshalFn = null;
        if (op.returns || op.outParams.length > 0) {
            unmarshalFn = function (asyncResult) {
                //
                // The results array holds the out parameters in the following format:
                //
                // [retval, out1, out2, ..., asyncResult]
                //
                const results = [];
 
                const is = asyncResult.startReadParams();
                let retvalInfo;
                if (op.returns && !op.returns.tag) {
                    retvalInfo = op.returns;
                }
                unmarshalParams(is, retvalInfo, op.outParams, op.outParamsOpt, op.returnsClasses, results);
                asyncResult.endReadParams();
                return results.length == 1 ? results[0] : results;
            };
        }
        return ObjectPrx._invoke(
            this,
            op.name,
            op.mode,
            op.format,
            ctx,
            marshalFn,
            unmarshalFn,
            op.exceptions,
            Array.prototype.slice.call(args),
        );
    };
}
 
export function defineOperations(classType, proxyType, ids, id, ops) {
    if (ops) {
        classType._iceOps = new OpTable(ops);
    }
 
    classType.prototype.dispatch = function (request) {
        //
        // Retrieve the dispatch method for this operation.
        //
        const method = getServantMethod(classType, request.current.operation);
 
        if (method === undefined || typeof method !== "function") {
            throw new OperationNotExistException();
        }
 
        return method.call(method, this, request);
    };
 
    classType.prototype._iceMostDerivedType = function () {
        return classType;
    };
 
    Object.defineProperty(classType, "_iceIds", {
        get: () => ids,
    });
 
    Object.defineProperty(classType, "_iceId", {
        get: () => id,
    });
 
    classType.ice_staticId = function () {
        return id;
    };
 
    if (proxyType !== undefined) {
        if (ops) {
            for (const name in ops) {
                addProxyOperation(proxyType, name, ops[name]);
            }
        }
 
        //
        // Copy proxy methods from super-interfaces.
        //
        if (proxyType._implements) {
            for (const intf in proxyType._implements) {
                const proto = proxyType._implements[intf].prototype;
                for (const f in proto) {
                    if (typeof proto[f] == "function" && proxyType.prototype[f] === undefined) {
                        proxyType.prototype[f] = proto[f];
                    }
                }
            }
        }
 
        Object.defineProperty(proxyType, "_id", {
            get: () => id,
        });
    }
}