12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.24/esri/copyright.txt for details.
- */
- import e from"./ArcadePortal.js";import t from"./Attachment.js";import r from"./Dictionary.js";import n from"./Feature.js";import o from"./FunctionWrapper.js";import a from"./ImmutablePathArray.js";import i from"./ImmutablePointArray.js";import{p as s,i as c,m as l,N as u,x as f,v as p,w,o as h,b as d,l as m,n as g,s as E,y,a as N,f as v,I,R as T,h as S,q as b,r as R,j as O,z as A,A as U}from"../chunks/languageUtils.js";import{nodeErrorMessage as C,addFunctionDeclaration as F,findFieldLiterals as x,validateScript as M,referencesMember as P,referencesFunction as D,findFunctionCalls as j}from"./treeAnalysis.js";import{A as L}from"../chunks/array.js";import{registerFunctions as k}from"./functions/date.js";import{registerFunctions as _}from"./functions/geomasync.js";import{registerFunctions as V}from"./functions/geometry.js";import{registerFunctions as Y}from"./functions/maths.js";import{registerFunctions as B}from"./functions/stats.js";import{registerFunctions as G}from"./functions/string.js";import z from"../geometry/Extent.js";import q from"../geometry/Geometry.js";import H from"../geometry/Multipoint.js";import Z from"../geometry/Point.js";import W from"../geometry/Polygon.js";import K from"../geometry/Polyline.js";import X from"../geometry/SpatialReference.js";function J(e){return e&&"function"==typeof e.then}const Q=100;async function $(e,t){const r=[];for(let n=0;n<t.arguments.length;n++)r.push(await re(e,t.arguments[n]));return r}async function ee(e,t,r){if(!0===t.preparsed)return r(e,null,t.arguments);return r(e,t,await $(e,t))}async function te(e,t,r){if(!0===t.preparsed){const n=r(e,null,t.arguments);return J(n),n}const n=r(e,t,await $(e,t));return J(n),n}async function re(e,t,r){if(t.breakpoint&&!0!==r){const r=t.breakpoint();return await r,re(e,t,!0)}switch(t.type){case"VariableDeclarator":return Re(e,t);case"VariableDeclaration":return be(e,t,0);case"BlockStatement":return ve(e,t);case"FunctionDeclaration":return Se(e,t);case"ReturnStatement":return Te(e,t);case"IfStatement":return Ne(e,t);case"ExpressionStatement":return ye(e,t);case"UpdateExpression":return me(e,t);case"AssignmentExpression":return Ee(e,t);case"ForStatement":return ce(e,t);case"ForInStatement":return de(e,t);case"BreakStatement":return w;case"EmptyStatement":return p;case"ContinueStatement":return f;case"TemplateElement":return je(e,t);case"TemplateLiteral":return Le(e,t);case"Identifier":return Pe(e,t);case"MemberExpression":return Ue(e,t);case"Literal":return t.value;case"CallExpression":return De(e,t);case"UnaryExpression":return Ce(e,t);case"BinaryExpression":return xe(e,t);case"LogicalExpression":return Me(e,t);case"ArrayExpression":return Fe(e,t);case"ObjectExpression":return ne(e,t);case"Property":return oe(e,t);default:throw new Error(C(t,"RUNTIME","UNREOGNISED"))}}async function ne(e,t){const n=[];for(let r=0;r<t.properties.length;r++)n[r]=await re(e,t.properties[r]);const o={};for(let r=0;r<n.length;r++){const e=n[r];if(h(e.value))throw new Error("Illegal Argument");if(!1===d(e.key))throw new Error("Illegal Argument");e.value===p?o[e.key.toString()]=null:o[e.key.toString()]=e.value}const a=new r(o);return a.immutable=!1,a}async function oe(e,t){const r=await re(e,t.value);if("Identifier"===t.key.type)return{key:t.key.name,value:r};return{key:await re(e,t.key),value:r}}async function ae(e,t,r){const n=await re(e,t.body);return r.lastAction=n,r.lastAction===w||r.lastAction instanceof T?(r.testResult=!1,r):null!==t.update?(await re(e,t.update),r):r}async function ie(e,t,r){if(null!==t.test){const n=await re(e,t.test);if(!0===e.abortSignal.aborted)throw new Error("Cancelled");if(r.testResult=n,!1===r.testResult)return r;if(!0!==r.testResult)throw new Error(C(t,"RUNTIME","CANNOT_USE_NONBOOLEAN_IN_CONDITION"));return ae(e,t,r)}return ae(e,t,r)}function se(e,t,r,n,o,a){try{ie(e,t,r).then((()=>{try{!0===r.testResult?++a>Q?(a=0,setTimeout((()=>{se(e,t,r,n,o,a)}),0)):se(e,t,r,n,o,a):r.lastAction instanceof T?n(r.lastAction):n(p)}catch(i){o(i)}}),(e=>{o(e)}))}catch(i){o(i)}}function ce(e,t){try{return null!==t.init?re(e,t.init).then((()=>new Promise(((r,n)=>{se(e,t,{testResult:!0,lastAction:p},(e=>{r(e)}),(e=>{n(e)}),0)})))):new Promise(((r,n)=>{se(e,t,{testResult:!0,lastAction:p},(e=>{r(e)}),(e=>{n(e)}),0)}))}catch(r){return Promise.reject(r)}}function le(e,t,r,n,o,a,i,s,c,l){try{if(n<=a)return void s(p);o.value="k"===i?r[a]:a,re(e,t.body).then((u=>{try{u instanceof T?s(u):u===w?s(p):++l>Q?(l=0,setTimeout((()=>{le(e,t,r,n,o,a+1,i,s,c,l)}),0)):le(e,t,r,n,o,a+1,i,s,c,l)}catch(f){c(f)}}),(e=>{c(e)}))}catch(u){c(u)}}function ue(e,t,r,n,o,a,i,s,c){try{if(r.length()<=o)return void i(p);n.value="k"===a?r.get(o):o,re(e,t.body).then((l=>{l instanceof T?i(l):l===w?i(p):++c>Q?(c=0,setTimeout((()=>{ue(e,t,r,n,o+1,a,i,s,c)}),0)):ue(e,t,r,n,o+1,a,i,s,c)}),(e=>{s(e)}))}catch(l){s(l)}}function fe(e,t,r,n,o,a){try{if(void 0===a&&(a="i"),0===r.length)return void n.resolve(p);le(e,t,r,r.length,o,0,a,(e=>{n.resolve(e)}),(e=>{n.reject(e)}),0)}catch(i){n.reject(i)}}function pe(e,t,r,n,o,a){try{if(void 0===a&&(a="i"),0===r.length)return void n.resolve(p);ue(e,t,r,o,0,a,(e=>{n.resolve(e)}),(e=>{n.reject(e)}),0)}catch(i){n.reject(i)}}function we(e,t,r,n,o){try{fe(e,t,r.keys(),n,o,"k")}catch(a){n.reject(a)}}function he(e,t,r,o,a,i,s,c){try{e.next().then((l=>{try{if(null===l)i(p);else{const u=n.createFromGraphicLikeObject(l.geometry,l.attributes,o);u._underlyingGraphic=l,a.value=u;re(t,r.body).then((n=>{try{n===w?i(p):n instanceof T?i(n):++c>Q?(c=0,setTimeout((()=>{he(e,t,r,o,a,i,s,c)}),0)):he(e,t,r,o,a,i,s,c)}catch(l){s(l)}}),(e=>{s(e)}))}}catch(u){s(u)}}),(e=>{s(e)}))}catch(l){s(l)}}async function de(e,t){return new Promise(((n,o)=>{re(e,t.right).then((a=>{try{let i=null;i="VariableDeclaration"===t.left.type?re(e,t.left):Promise.resolve(),i.then((()=>{try{let i="";if("VariableDeclaration"===t.left.type){const e=t.left.declarations[0].id;"Identifier"===e.type&&(i=e.name)}else"Identifier"===t.left.type&&(i=t.left.name);if(!i)throw new Error(C(t,"RUNTIME","INVALIDVARIABLE"));i=i.toLowerCase();let s=null;if(null!==e.localScope&&void 0!==e.localScope[i]&&(s=e.localScope[i]),null===s&&void 0!==e.globalScope[i]&&(s=e.globalScope[i]),null===s)return void o(new Error(C(t,"RUNTIME","VARIABLENOTDECLARED")));m(a)||d(a)?fe(e,t,a,{reject:o,resolve:n},s):g(a)?pe(e,t,a,{reject:o,resolve:n},s):a instanceof r||E(a)?we(e,t,a,{reject:o,resolve:n},s):y(a)?he(a.iterator(e.abortSignal),e,t,a,s,(e=>{n(e)}),(e=>{o(e)}),0):fe(e,t,[],{reject:o,resolve:n},s)}catch(i){o(i)}}),o)}catch(i){o(i)}}),o)}))}async function me(e,t){const n=t.argument;if("MemberExpression"===n.type){const o={t:null},a=await re(e,n.object);let i=null;o.t=a,!0===n.computed?i=await re(e,n.property):"Identifier"===n.property.type&&(i=n.property.name);const s=o.t;let c;if(m(s)){if(!N(i))throw new Error("Invalid Parameter");if(i<0&&(i=s.length+i),i<0||i>=s.length)throw new Error("Assignment outside of array bounds");c=v(s[i]),s[i]="++"===t.operator?c+1:c-1}else if(s instanceof r){if(!1===d(i))throw new Error("Dictionary accessor must be a string");if(!0!==s.hasField(i))throw new Error("Invalid Parameter");c=v(s.field(i)),s.setField(i,"++"===t.operator?c+1:c-1)}else{if(!E(s))throw g(s)?new Error("Array is Immutable"):new Error("Invalid Parameter");if(!1===d(i))throw new Error("Feature accessor must be a string");if(!0!==s.hasField(i))throw new Error("Invalid Parameter");c=v(s.field(i)),s.setField(i,"++"===t.operator?c+1:c-1)}return!1===t.prefix?c:"++"===t.operator?c+1:c-1}const o="Identifier"===t.argument.type?t.argument.name.toLowerCase():"";if(!o)throw new Error("Invalid identifier");let a;if(null!==e.localScope&&void 0!==e.localScope[o])return a=v(e.localScope[o].value),e.localScope[o]={value:"++"===t.operator?a+1:a-1,valueset:!0,node:t},!1===t.prefix?a:"++"===t.operator?a+1:a-1;if(void 0!==e.globalScope[o])return a=v(e.globalScope[o].value),e.globalScope[o]={value:"++"===t.operator?a+1:a-1,valueset:!0,node:t},!1===t.prefix?a:"++"===t.operator?a+1:a-1;throw new Error("Variable not recognised")}function ge(e,t,r,n){switch(t){case"=":return e===p?null:e;case"/=":return v(r)/v(e);case"*=":return v(r)*v(e);case"-=":return v(r)-v(e);case"+=":return d(r)||d(e)?S(r)+S(e):v(r)+v(e);case"%=":return v(r)%v(e);default:throw new Error(C(n,"RUNTIME","OPERATORNOTRECOGNISED"))}}async function Ee(e,t){const n=t.left;if("MemberExpression"===n.type){const o=await re(e,t.right),a=await re(e,n.object);let i=null;if(!0===n.computed)i=await re(e,n.property);else{if("Identifier"!==n.property.type)throw new Error("Expected computed or identifier for assignemnt target");i=n.property.name}if(m(a)){if(!N(i))throw new Error("Invalid Parameter");if(i<0&&(i=a.length+i),i<0||i>a.length)throw new Error("Assignment outside of array bounds");if(i===a.length){if("="!==t.operator)throw new Error("Invalid Parameter");a[i]=ge(o,t.operator,a[i],t)}else a[i]=ge(o,t.operator,a[i],t)}else if(a instanceof r){if(!1===d(i))throw new Error("Dictionary accessor must be a string");if(!0===a.hasField(i))a.setField(i,ge(o,t.operator,a.field(i),t));else{if("="!==t.operator)throw new Error("Invalid Parameter");a.setField(i,ge(o,t.operator,null,t))}}else{if(!E(a))throw g(a)?new Error("Array is Immutable"):new Error("Invalid Parameter");if(!1===d(i))throw new Error("Feature accessor must be a string");if(!0===a.hasField(i))a.setField(i,ge(o,t.operator,a.field(i),t));else{if("="!==t.operator)throw new Error("Invalid Parameter");a.setField(i,ge(o,t.operator,null,t))}}return p}const o=n.name.toLowerCase();if(null!==e.localScope&&void 0!==e.localScope[o]){const r=await re(e,t.right);return e.localScope[o]={value:ge(r,t.operator,e.localScope[o].value,t),valueset:!0,node:t.right},p}if(void 0!==e.globalScope[o]){const r=await re(e,t.right);return e.globalScope[o]={value:ge(r,t.operator,e.globalScope[o].value,t),valueset:!0,node:t.right},p}throw new Error("Cannot assign undeclared variable")}async function ye(e,t){if("AssignmentExpression"===t.expression.type)return re(e,t.expression);if("CallExpression"===t.expression.type){const r=await re(e,t.expression);return r===p?p:new I(r)}const r=await re(e,t.expression);return r===p?p:new I(r)}async function Ne(e,t){if("AssignmentExpression"===t.test.type||"UpdateExpression"===t.test.type)throw new Error(C(t.test,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));const r=await re(e,t.test);if(!0===r)return re(e,t.consequent);if(!1===r)return null!==t.alternate?re(e,t.alternate):p;throw new Error(C(t.test,"RUNTIME","CANNOT_USE_NONBOOLEAN_IN_CONDITION"))}async function ve(e,t){return Ie(e,t,0)}async function Ie(e,t,r){if(r>=t.body.length)return p;const n=await re(e,t.body[r]);return n instanceof T||n===w||n===f||r===t.body.length-1?n:Ie(e,t,r+1)}async function Te(e,t){if(null===t.argument)return new T(p);const r=await re(e,t.argument);return new T(r)}async function Se(e,t){const r=t.id.name.toLowerCase();return e.globalScope[r]={valueset:!0,node:null,value:new o(t,e)},p}async function be(e,t,r){return r>=t.declarations.length?p:(await re(e,t.declarations[r]),r===t.declarations.length-1||await be(e,t,r+1),p)}async function Re(e,t){let r=null;if(r=null===t.init?null:await re(e,t.init),null!==e.localScope){if(r===p&&(r=null),"Identifier"!==t.id.type)throw new Error("Can only assign a regular variable");const n=t.id.name.toLowerCase();return e.localScope[n]={value:r,valueset:!0,node:t.init},p}if("Identifier"!==t.id.type)throw new Error("Can only assign a regular variable");const n=t.id.name.toLowerCase();return r===p&&(r=null),e.globalScope[n]={value:r,valueset:!0,node:t.init},p}let Oe=0;function Ae(e,t,n,o){let s;switch(t=t.toLowerCase()){case"hasz":{const t=e.hasZ;return void 0!==t&&t}case"hasm":{const t=e.hasM;return void 0!==t&&t}case"spatialreference":{let t=e.spatialReference._arcadeCacheId;if(void 0===t){let r=!0;Object.freeze&&Object.isFrozen(e.spatialReference)&&(r=!1),r&&(Oe++,e.spatialReference._arcadeCacheId=Oe,t=Oe)}const n=new r({wkt:e.spatialReference.wkt,wkid:e.spatialReference.wkid});return void 0!==t&&(n._arcadeCacheId="SPREF"+t.toString()),n}}switch(e.type){case"extent":switch(t){case"xmin":case"xmax":case"ymin":case"ymax":case"zmin":case"zmax":case"mmin":case"mmax":{const r=e[t];return void 0!==r?r:null}case"type":return"Extent"}break;case"polygon":switch(t){case"rings":s=e.cache._arcadeCacheId,void 0===s&&(Oe++,s=Oe,e.cache._arcadeCacheId=s);return new a(e.rings,e.spatialReference,!0===e.hasZ,!0===e.hasM,s);case"type":return"Polygon"}break;case"point":switch(t){case"x":case"y":case"z":case"m":return void 0!==e[t]?e[t]:null;case"type":return"Point"}break;case"polyline":switch(t){case"paths":s=e.cache._arcadeCacheId,void 0===s&&(Oe++,s=Oe,e.cache._arcadeCacheId=s);return new a(e.paths,e.spatialReference,!0===e.hasZ,!0===e.hasM,s);case"type":return"Polyline"}break;case"multipoint":switch(t){case"points":s=e.cache._arcadeCacheId,void 0===s&&(Oe++,s=Oe,e.cache._arcadeCacheId=s);return new i(e.points,e.spatialReference,!0===e.hasZ,!0===e.hasM,s,1);case"type":return"Multipoint"}}throw new Error(C(o,"RUNTIME","PROPERTYNOTFOUND"))}async function Ue(e,t){const n=await re(e,t.object);if(null===n)throw new Error(C(t,"RUNTIME","NOTFOUND"));if(!1===t.computed){if("Identifier"===t.property.type){if(n instanceof r||E(n))return n.field(t.property.name);if(n instanceof q)return Ae(n,t.property.name,e,t);throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}let o=await re(e,t.property);if(n instanceof r||E(n)){if(d(o))return n.field(o);throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(n instanceof q){if(d(o))return Ae(n,o,e,t);throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(m(n)){if(N(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length+o),o>=n.length||o<0)throw new Error(C(t,"RUNTIME","OUTOFBOUNDS"));return n[o]}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(g(n)){if(N(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length()+o),o>=n.length()||o<0)throw new Error(C(t,"RUNTIME","OUTOFBOUNDS"));return n.get(o)}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}if(d(n)){if(N(o)&&isFinite(o)&&Math.floor(o)===o){if(o<0&&(o=n.length+o),o>=n.length||o<0)throw new Error(C(t,"RUNTIME","OUTOFBOUNDS"));return n[o]}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}throw new Error(C(t,"RUNTIME","INVALIDTYPE"))}async function Ce(e,t){const r=await re(e,t.argument);if(c(r)){if("!"===t.operator)return!r;if("-"===t.operator)return-1*v(r);if("+"===t.operator)return 1*v(r);if("~"===t.operator)return~v(r);throw new Error(C(t,"RUNTIME","NOTSUPPORTEDUNARYOPERATOR"))}if("-"===t.operator)return-1*v(r);if("+"===t.operator)return 1*v(r);if("~"===t.operator)return~v(r);throw new Error(C(t,"RUNTIME","NOTSUPPORTEDUNARYOPERATOR"))}async function Fe(e,t){const r=[];for(let n=0;n<t.elements.length;n++)r.push(await re(e,t.elements[n]));for(let n=0;n<r.length;n++){if(h(r[n]))throw new Error(C(t,"RUNTIME","FUNCTIONCONTEXTILLEGAL"));r[n]===p&&(r[n]=null)}return r}async function xe(e,t){const r=[];r[0]=await re(e,t.left),r[1]=await re(e,t.right);const n=r[0],o=r[1];switch(t.operator){case"|":case"<<":case">>":case">>>":case"^":case"&":return R(v(n),v(o),t.operator);case"==":return l(n,o);case"!=":return!l(n,o);case"<":case">":case"<=":case">=":return b(n,o,t.operator);case"+":return d(n)||d(o)?S(n)+S(o):v(n)+v(o);case"-":return v(n)-v(o);case"*":return v(n)*v(o);case"/":return v(n)/v(o);case"%":return v(n)%v(o);default:throw new Error(C(t,"RUNTIME","OPERATORNOTRECOGNISED"))}}async function Me(e,t){if("AssignmentExpression"===t.left.type||"UpdateExpression"===t.left.type)throw new Error(C(t.left,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));if("AssignmentExpression"===t.right.type||"UpdateExpression"===t.right.type)throw new Error(C(t.right,"RUNTIME","CANNOT_USE_ASSIGNMENT_IN_CONDITION"));const r=await re(e,t.left);let n=null;if(!c(r))throw new Error(C(t,"RUNTIME","ONLYBOOLEAN"));switch(t.operator){case"||":if(!0===r)return r;if(n=await re(e,t.right),c(n))return n;throw new Error(C(t,"RUNTIME","ONLYORORAND"));case"&&":if(!1===r)return r;if(n=await re(e,t.right),c(n))return n;throw new Error(C(t,"RUNTIME","ONLYORORAND"));default:throw new Error(C(t,"RUNTIME","ONLYORORAND"))}}async function Pe(e,t){const r=t.name.toLowerCase();if(null!==e.localScope&&void 0!==e.localScope[r]){const t=e.localScope[r];if(!0===t.valueset)return t.value;if(null!==t.d)return t.d;t.d=re(e,t.node);const n=await t.d;return t.value=n,t.valueset=!0,n}if(void 0!==e.globalScope[r]){const t=e.globalScope[r];if(!0===t.valueset)return t.value;if(null!==t.d)return t.d;t.d=re(e,t.node);const n=await t.d;return t.value=n,t.valueset=!0,n}throw new Error(C(t,"RUNTIME","VARIABLENOTFOUND"))}async function De(e,t){if("Identifier"!==t.callee.type)throw new Error(C(t,"RUNTIME","ONLYNODESSUPPORTED"));if(null!==e.localScope&&void 0!==e.localScope[t.callee.name.toLowerCase()]){const r=e.localScope[t.callee.name.toLowerCase()];if(r.value instanceof u)return r.value.fn(e,t);if(r.value instanceof o)return Ze(e,t,r.value.definition);throw new Error(C(t,"RUNTIME","NOTAFUNCTION"))}if(void 0!==e.globalScope[t.callee.name.toLowerCase()]){const r=e.globalScope[t.callee.name.toLowerCase()];if(r.value instanceof u)return r.value.fn(e,t);if(r.value instanceof o)return Ze(e,t,r.value.definition);throw new Error(C(t,"RUNTIME","NOTAFUNCTION"))}throw new Error(C(t,"RUNTIME","NOTFOUND"))}async function je(e,t){return t.value?t.value.cooked:""}async function Le(e,t){const r=[];for(let a=0;a<t.expressions.length;a++){const n=await re(e,t.expressions[a]);r[a]=S(n)}let n="",o=0;for(const a of t.quasis)if(n+=a.value?a.value.cooked:"",!1===a.tail){n+=r[o]?r[o]:"",o++}return n}const ke={};function _e(n){return null===n?"":m(n)||g(n)?"Array":O(n)?"Date":d(n)?"String":c(n)?"Boolean":N(n)?"Number":n instanceof t?"Attachment":n instanceof e?"Portal":n instanceof r?"Dictionary":E(n)?"Feature":n instanceof Z?"Point":n instanceof W?"Polygon":n instanceof K?"Polyline":n instanceof H?"Multipoint":n instanceof z?"Extent":h(n)?"Function":y(n)?"FeatureSet":A(n)?"FeatureSetCollection":n===p?"":"number"==typeof n&&isNaN(n)?"Number":"Unrecognised Type"}async function Ve(e,t,r,n){const o=await re(e,t.arguments[r]);if(l(o,n))return re(e,t.arguments[r+1]);const a=t.arguments.length-r;return 1===a?re(e,t.arguments[r]):2===a?null:3===a?re(e,t.arguments[r+2]):Ve(e,t,r+2,n)}async function Ye(e,t,r,n){if(!0===n)return re(e,t.arguments[r+1]);if(3===t.arguments.length-r)return re(e,t.arguments[r+2]);const o=await re(e,t.arguments[r+2]);if(!1===c(o))throw new Error("WHEN needs boolean test conditions");return Ye(e,t,r+2,o)}async function Be(e,t){const r=e.length,n=Math.floor(r/2);if(0===r)return[];if(1===r)return[e[0]];const o=[Be(e.slice(0,n),t),Be(e.slice(n,r),t)],a=await Promise.all(o);return Ge(a[0],a[1],t,[])}async function Ge(e,t,r,n){const o=n;if(!(e.length>0||t.length>0))return n;if(e.length>0&&t.length>0){let a=await r(e[0],t[0]);return isNaN(a)&&(a=1),a<=0?(o.push(e[0]),e=e.slice(1)):(o.push(t[0]),t=t.slice(1)),Ge(e,t,r,n)}return e.length>0?(o.push(e[0]),Ge(e=e.slice(1),t,r,n)):t.length>0?(o.push(t[0]),Ge(e,t=t.slice(1),r,n)):void 0}function ze(e,t){const r=e.length,n=Math.floor(r/2);return t||(t=function(e,t){return e<t?-1:e===t?0:1}),0===r?[]:1===r?[e[0]]:qe(ze(e.slice(0,n),t),ze(e.slice(n,r),t),t)}function qe(e,t,r){const n=[];for(;e.length>0||t.length>0;)if(e.length>0&&t.length>0){let o=r(e[0],t[0]);isNaN(o)&&(o=1),o<=0?(n.push(e[0]),e=e.slice(1)):(n.push(t[0]),t=t.slice(1))}else e.length>0?(n.push(e[0]),e=e.slice(1)):t.length>0&&(n.push(t[0]),t=t.slice(1));return n}async function He(e,t,r){const n=e.body;if(r.length!==e.params.length)throw new Error("Invalid Parameter calls to function.");for(let a=0;a<r.length;a++){const n=e.params[a];"Identifier"===n.type&&(t.localScope[n.name.toLowerCase()]={d:null,value:r[a],valueset:!0,node:null})}const o=await re(t,n);if(o instanceof T)return o.value;if(o===w)throw new Error("Cannot Break from a Function");if(o===f)throw new Error("Cannot Continue from a Function");return o instanceof I?o.value:o}function Ze(e,t,r){return te(e,t,(function(t,n,o){const a={spatialReference:e.spatialReference,services:e.services,console:e.console,lrucache:e.lrucache,interceptor:e.interceptor,localScope:{},abortSignal:e.abortSignal,globalScope:e.globalScope,depthCounter:e.depthCounter+1};if(a.depthCounter>64)throw new Error("Exceeded maximum function depth");return He(r,a,o)}))}function We(e){const t=function(){const t={abortSignal:e.context.abortSignal,spatialReference:e.context.spatialReference,console:e.context.console,lrucache:e.context.lrucache,interceptor:e.context.interceptor,services:e.context.services,localScope:{},globalScope:e.context.globalScope,depthCounter:e.context.depthCounter+1};if(t.depthCounter>64)throw new Error("Exceeded maximum function depth");return He(e.definition,t,arguments)};return t}k(ke,ee),G(ke,ee),Y(ke,ee),V(ke,ee),B(ke,ee),_({functions:ke,compiled:!1,signatures:null,evaluateIdentifier:null,arcadeCustomFunctionHandler:null,mode:"async",standardFunction:ee,standardFunctionAsync:te}),ke.typeof=function(e,t){return ee(e,t,(function(e,t,r){s(r,1,1);const n=_e(r[0]);if("Unrecognised Type"===n)throw new Error("Unrecognised Type");return n}))},ke.iif=async function(e,t){s(null===t.arguments?[]:t.arguments,3,3);const r=await re(e,t.arguments[0]);if(!1===c(r))throw new Error("IF Function must have a boolean test condition");const n=[];return n[0]=await re(e,t.arguments[1]),n[1]=await re(e,t.arguments[2]),r?n[0]:n[1]},ke.decode=async function(e,t){if(t.arguments.length<2)throw new Error("Missing Parameters");if(2===t.arguments.length)return re(e,t.arguments[1]);if((t.arguments.length-1)%2==0)throw new Error("Must have a default value result.");return Ve(e,t,1,await re(e,t.arguments[0]))},ke.when=async function(e,t){if(t.arguments.length<3)throw new Error("Missing Parameters");if(t.arguments.length%2==0)throw new Error("Must have a default value result.");const r=await re(e,t.arguments[0]);if(!1===c(r))throw new Error("WHEN needs boolean test conditions");return Ye(e,t,0,r)},ke.sort=function(e,t){return te(e,t,(async function(e,t,r){s(r,1,2);let n=r[0];if(g(n)&&(n=n.toArray()),!1===m(n))throw new Error("Illegal Argument");if(r.length>1){if(!1===h(r[1]))throw new Error("Illegal Argument");return Be(n,We(r[1]))}let o=n;if(0===o.length)return[];const a={};for(let s=0;s<o.length;s++){const e=_e(o[s]);""!==e&&(a[e]=!0)}if(!0===a.Array||!0===a.Dictionary||!0===a.Feature||!0===a.Point||!0===a.Polygon||!0===a.Polyline||!0===a.Multipoint||!0===a.Extent||!0===a.Function)return o.slice(0);let i=0,c="";for(const s in a)i++,c=s;return i>1||"String"===c?o=ze(o,(function(e,t){if(null==e||e===p)return null==t||t===p?0:1;if(null==t||t===p)return-1;const r=S(e),n=S(t);return r<n?-1:r===n?0:1})):"Number"===c?o=ze(o,(function(e,t){return e-t})):"Boolean"===c?o=ze(o,(function(e,t){return e===t?0:t?-1:1})):"Date"===c&&(o=ze(o,(function(e,t){return t-e}))),o}))};const Ke={fixSpatialReference:U,parseArguments:$,standardFunction:ee,standardFunctionAsync:te,evaluateIdentifier:Pe,arcadeCustomFunction:We};for(const st in ke)ke[st]={value:new u(ke[st]),valueset:!0,node:null};const Xe=function(){};function Je(e,t){const o=new Xe;null==e&&(e={}),null==t&&(t={});const a=new r({newline:"\n",tab:"\t",singlequote:"'",doublequote:'"',forwardslash:"/",backwardslash:"\\"});a.immutable=!1,o.textformatting={value:a,valueset:!0,node:null};for(const r in t)o[r]={value:new u(t[r]),native:!0,valueset:!0,node:null};for(const r in e)e[r]&&"esri.Graphic"===e[r].declaredClass?o[r]={value:n.createFromGraphic(e[r]),valueset:!0,node:null}:o[r]={value:e[r],valueset:!0,node:null};return o}function Qe(e){console.log(e)}Xe.prototype=ke,Xe.prototype.infinity={value:Number.POSITIVE_INFINITY,valueset:!0,node:null},Xe.prototype.pi={value:Math.PI,valueset:!0,node:null};const $e=Ke;function et(e){const t={mode:"async",compiled:!1,functions:{},signatures:[],standardFunction:ee,standardFunctionAsync:te,evaluateIdentifier:Pe,arcadeCustomFunctionHandler:We};for(let r=0;r<e.length;r++)e[r].registerFunctions(t);for(const r in t.functions)ke[r]={value:new u(t.functions[r]),valueset:!0,node:null},Xe.prototype[r]=ke[r];for(let r=0;r<t.signatures.length;r++)F(t.signatures[r],"async")}async function tt(e,t){let r=t.spatialReference;null==r&&(r=new X({wkid:102100}));const n=Je(t.vars,t.customfunctions),a={spatialReference:r,services:t.services,abortSignal:void 0===t.abortSignal||null===t.abortSignal?{aborted:!1}:t.abortSignal,globalScope:n,console:t.console?t.console:Qe,lrucache:t.lrucache,interceptor:t.interceptor,localScope:null,depthCounter:1};let i=await re(a,e.body[0].body);if(i instanceof T&&(i=i.value),i instanceof I&&(i=i.value),i===p&&(i=null),i===w)throw new Error("Cannot return BREAK");if(i===f)throw new Error("Cannot return CONTINUE");if(i instanceof u)throw new Error("Cannot return FUNCTION");if(i instanceof o)throw new Error("Cannot return FUNCTION");return i}function rt(e,t){return x(e)}function nt(e,t){return M(e,t,"full")}function ot(e,t){return P(e,t)}function at(e,t){return D(e,t)}function it(e){return j(e)}et([L]);export{tt as executeScript,et as extend,rt as extractFieldLiterals,it as findFunctionCalls,$e as functionHelper,at as referencesFunction,ot as referencesMember,nt as validateScript};
|