git.delta.rocks / jrsonnet / refs/commits / ab1fc1bfe37e

difftreelog

feat support legacy std.thisFile

Yaroslav Bolyukin2022-04-22parent: #e082575.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -140,6 +140,11 @@
 	#[error("sort error: {0}")]
 	Sort(#[from] SortError),
 
+	/// Thrown as error, as this is legacy feature, and error here
+	/// is acceptable for defeating object field cache
+	#[error("should not reach outside: std.thisFile")]
+	MagicThisFileUsed,
+
 	#[cfg(feature = "anyhow-error")]
 	#[error(transparent)]
 	Other(Rc<anyhow::Error>),
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -568,12 +568,13 @@
 				(Val::Obj(v), Val::Str(key)) => s.push(
 					CallLocation::new(loc),
 					|| format!("field <{}> access", key),
-					|| {
-						if let Some(v) = v.get(s.clone(), key.clone())? {
-							Ok(v)
-						} else {
-							throw!(NoSuchField(key.clone()))
+					|| match v.get(s.clone(), key.clone()) {
+						Ok(Some(v)) => Ok(v),
+						Ok(None) => throw!(NoSuchField(key.clone())),
+						Err(e) if matches!(e.error(), MagicThisFileUsed) => {
+							Ok(Val::Str(loc.0.to_string_lossy().into()))
 						}
+						Err(e) => Err(e),
 					},
 				)?,
 				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(
@@ -674,6 +675,7 @@
 				.with(|b| b.get(name).copied())
 				.ok_or_else(|| IntrinsicNotFound(name.clone()))?,
 		)),
+		IntrinsicThisFile => return Err(MagicThisFileUsed.into()),
 		AssertExpr(assert, returned) => {
 			evaluate_assert(s.clone(), ctx.clone(), assert)?;
 			evaluate(s, ctx, returned)?
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -296,6 +296,8 @@
 	Index(LocExpr, LocExpr),
 	/// function(x) x
 	Function(ParamsDesc, LocExpr),
+	/// std.thisFile
+	IntrinsicThisFile,
 	/// std.primitiveEquals
 	Intrinsic(IStr),
 	/// if true == false then 1 else 2
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -210,6 +210,7 @@
 		pub rule expr_basic(s: &ParserSettings) -> Expr
 			= literal(s)
 
+			/ quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}}
 			/ quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}
 
 			/ string_expr(s) / number_expr(s)
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Those functions aren't normally located in stdlib6  length:: $intrinsic(length),7  type:: $intrinsic(type),8  makeArray:: $intrinsic(makeArray),9  codepoint:: $intrinsic(codepoint),10  objectFieldsEx:: $intrinsic(objectFieldsEx),11  objectHasEx:: $intrinsic(objectHasEx),12  primitiveEquals:: $intrinsic(primitiveEquals),13  modulo:: $intrinsic(modulo),14  floor:: $intrinsic(floor),15  ceil:: $intrinsic(ceil),16  extVar:: $intrinsic(extVar),17  native:: $intrinsic(native),18  filter:: $intrinsic(filter),19  char:: $intrinsic(char),20  encodeUTF8:: $intrinsic(encodeUTF8),21  decodeUTF8:: $intrinsic(decodeUTF8),22  md5:: $intrinsic(md5),23  trace:: $intrinsic(trace),24  id:: $intrinsic(id),25  parseJson:: $intrinsic(parseJson),26  parseYaml:: $intrinsic(parseYaml),2728  log:: $intrinsic(log),29  pow:: $intrinsic(pow),30  sqrt:: $intrinsic(sqrt),3132  sin:: $intrinsic(sin),33  cos:: $intrinsic(cos),34  tan:: $intrinsic(tan),35  asin:: $intrinsic(asin),36  acos:: $intrinsic(acos),37  atan:: $intrinsic(atan),3839  exp:: $intrinsic(exp),40  mantissa:: $intrinsic(mantissa),41  exponent:: $intrinsic(exponent),4243  any:: $intrinsic(any),44  all:: $intrinsic(all),4546  isString(v):: std.type(v) == 'string',47  isNumber(v):: std.type(v) == 'number',48  isBoolean(v):: std.type(v) == 'boolean',49  isObject(v):: std.type(v) == 'object',50  isArray(v):: std.type(v) == 'array',51  isFunction(v):: std.type(v) == 'function',5253  toString(a)::54    if std.type(a) == 'string' then a else '' + a,5556  substr:: $intrinsic(substr),5758  startsWith(a, b)::59    if std.length(a) < std.length(b) then60      false61    else62      std.substr(a, 0, std.length(b)) == b,6364  endsWith(a, b)::65    if std.length(a) < std.length(b) then66      false67    else68      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6970  lstripChars(str, chars)::71    if std.length(str) > 0 && std.member(chars, str[0]) then72      std.lstripChars(str[1:], chars)73    else74      str,7576  rstripChars(str, chars)::77    local len = std.length(str);78    if len > 0 && std.member(chars, str[len - 1]) then79      std.rstripChars(str[:len - 1], chars)80    else81      str,8283  stripChars(str, chars)::84    std.lstripChars(std.rstripChars(str, chars), chars),8586  stringChars(str)::87    std.makeArray(std.length(str), function(i) str[i]),8889  local parse_nat(str, base) =90    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;91    // These codepoints are in ascending order:92    local zero_code = std.codepoint('0');93    local upper_a_code = std.codepoint('A');94    local lower_a_code = std.codepoint('a');95    local addDigit(aggregate, char) =96      local code = std.codepoint(char);97      local digit = if code >= lower_a_code then98        code - lower_a_code + 1099      else if code >= upper_a_code then100        code - upper_a_code + 10101      else102        code - zero_code;103      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];104      base * aggregate + digit;105    std.foldl(addDigit, std.stringChars(str), 0),106107  parseInt(str)::108    assert std.isString(str) : 'Expected string, got ' + std.type(str);109    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];110    if str[0] == '-' then111      -parse_nat(str[1:], 10)112    else113      parse_nat(str, 10),114115  parseOctal(str)::116    assert std.isString(str) : 'Expected string, got ' + std.type(str);117    assert std.length(str) > 0 : 'Not an octal number: ""';118    parse_nat(str, 8),119120  parseHex(str)::121    assert std.isString(str) : 'Expected string, got ' + std.type(str);122    assert std.length(str) > 0 : 'Not hexadecimal: ""';123    parse_nat(str, 16),124125  split(str, c):: std.splitLimit(str, c, -1),126127  splitLimit:: $intrinsic(splitLimit),128129  strReplace:: $intrinsic(strReplace),130131  asciiUpper:: $intrinsic(asciiUpper),132133  asciiLower:: $intrinsic(asciiLower),134135  range:: $intrinsic(range),136137  repeat(what, count)::138    local joiner =139      if std.isString(what) then ''140      else if std.isArray(what) then []141      else error 'std.repeat first argument must be an array or a string';142    std.join(joiner, std.makeArray(count, function(i) what)),143144  slice:: $intrinsic(slice),145146  member:: $intrinsic(member),147148  count:: $intrinsic(count),149150  mod:: $intrinsic(mod),151152  map:: $intrinsic(map),153154  mapWithIndex(func, arr)::155    if !std.isFunction(func) then156      error ('std.mapWithIndex first param must be function, got ' + std.type(func))157    else if !std.isArray(arr) && !std.isString(arr) then158      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))159    else160      std.makeArray(std.length(arr), function(i) func(i, arr[i])),161162  mapWithKey(func, obj)::163    if !std.isFunction(func) then164      error ('std.mapWithKey first param must be function, got ' + std.type(func))165    else if !std.isObject(obj) then166      error ('std.mapWithKey second param must be object, got ' + std.type(obj))167    else168      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },169170  flatMap:: $intrinsic(flatMap),171172  join:: $intrinsic(join),173174  lines(arr)::175    std.join('\n', arr + ['']),176177  deepJoin(arr)::178    if std.isString(arr) then179      arr180    else if std.isArray(arr) then181      std.join('', [std.deepJoin(x) for x in arr])182    else183      error 'Expected string or array, got %s' % std.type(arr),184185186  format:: $intrinsic(format),187188  foldr:: $intrinsic(foldr),189190  foldl:: $intrinsic(foldl),191192  filterMap(filter_func, map_func, arr)::193    if !std.isFunction(filter_func) then194      error ('std.filterMap first param must be function, got ' + std.type(filter_func))195    else if !std.isFunction(map_func) then196      error ('std.filterMap second param must be function, got ' + std.type(map_func))197    else if !std.isArray(arr) then198      error ('std.filterMap third param must be array, got ' + std.type(arr))199    else200      std.map(map_func, std.filter(filter_func, arr)),201202  assertEqual(a, b)::203    if a == b then204      true205    else206      error 'Assertion failed. ' + a + ' != ' + b,207208  abs(n)::209    if !std.isNumber(n) then210      error 'std.abs expected number, got ' + std.type(n)211    else212      if n > 0 then n else -n,213214  sign(n)::215    if !std.isNumber(n) then216      error 'std.sign expected number, got ' + std.type(n)217    else218      if n > 0 then219        1220      else if n < 0 then221        -1222      else 0,223224  max(a, b)::225    if !std.isNumber(a) then226      error 'std.max first param expected number, got ' + std.type(a)227    else if !std.isNumber(b) then228      error 'std.max second param expected number, got ' + std.type(b)229    else230      if a > b then a else b,231232  min(a, b)::233    if !std.isNumber(a) then234      error 'std.min first param expected number, got ' + std.type(a)235    else if !std.isNumber(b) then236      error 'std.min second param expected number, got ' + std.type(b)237    else238      if a < b then a else b,239240  clamp(x, minVal, maxVal)::241    if x < minVal then minVal242    else if x > maxVal then maxVal243    else x,244245  flattenArrays(arrs)::246    std.foldl(function(a, b) a + b, arrs, []),247248  manifestIni(ini)::249    local body_lines(body) =250      std.join([], [251        local value_or_values = body[k];252        if std.isArray(value_or_values) then253          ['%s = %s' % [k, value] for value in value_or_values]254        else255          ['%s = %s' % [k, value_or_values]]256257        for k in std.objectFields(body)258      ]);259260    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),261          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],262          all_sections = [263      section_lines(k, ini.sections[k])264      for k in std.objectFields(ini.sections)265    ];266    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),267268  manifestToml(value):: std.manifestTomlEx(value, '  '),269270  manifestTomlEx(value, indent)::271    local272      escapeStringToml = std.escapeStringJson,273      escapeKeyToml(key) =274        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));275        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),276      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),277      isSection(v) = std.isObject(v) || isTableArray(v),278      renderValue(v, indexedPath, inline, cindent) =279        if v == true then280          'true'281        else if v == false then282          'false'283        else if v == null then284          error 'Tried to manifest "null" at ' + indexedPath285        else if std.isNumber(v) then286          '' + v287        else if std.isString(v) then288          escapeStringToml(v)289        else if std.isFunction(v) then290          error 'Tried to manifest function at ' + indexedPath291        else if std.isArray(v) then292          if std.length(v) == 0 then293            '[]'294          else295            local range = std.range(0, std.length(v) - 1);296            local new_indent = if inline then '' else cindent + indent;297            local separator = if inline then ' ' else '\n';298            local lines = ['[' + separator]299                          + std.join([',' + separator],300                                     [301                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]302                                       for i in range303                                     ])304                          + [separator + (if inline then '' else cindent) + ']'];305            std.join('', lines)306        else if std.isObject(v) then307          local lines = ['{ ']308                        + std.join([', '],309                                   [310                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]311                                     for k in std.objectFields(v)312                                   ])313                        + [' }'];314          std.join('', lines),315      renderTableInternal(v, path, indexedPath, cindent) =316        local kvp = std.flattenArrays([317          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]318          for k in std.objectFields(v)319          if !isSection(v[k])320        ]);321        local sections = [std.join('\n', kvp)] + [322          (323            if std.isObject(v[k]) then324              renderTable(v[k], path + [k], indexedPath + [k], cindent)325            else326              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)327          )328          for k in std.objectFields(v)329          if isSection(v[k])330        ];331        std.join('\n\n', sections),332      renderTable(v, path, indexedPath, cindent) =333        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'334        + (if v == {} then '' else '\n')335        + renderTableInternal(v, path, indexedPath, cindent + indent),336      renderTableArray(v, path, indexedPath, cindent) =337        local range = std.range(0, std.length(v) - 1);338        local sections = [339          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'340           + (if v[i] == {} then '' else '\n')341           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))342          for i in range343        ];344        std.join('\n\n', sections);345    if std.isObject(value) then346      renderTableInternal(value, [], [], '')347    else348      error 'TOML body must be an object. Got ' + std.type(value),349350  escapeStringJson:: $intrinsic(escapeStringJson),351352  escapeStringPython(str)::353    std.escapeStringJson(str),354355  escapeStringBash(str_)::356    local str = std.toString(str_);357    local trans(ch) =358      if ch == "'" then359        "'\"'\"'"360      else361        ch;362    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),363364  escapeStringDollars(str_)::365    local str = std.toString(str_);366    local trans(ch) =367      if ch == '$' then368        '$$'369      else370        ch;371    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),372373  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,374375  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),376377  manifestJsonEx:: $intrinsic(manifestJsonEx),378379  manifestYamlDoc:: $intrinsic(manifestYamlDoc),380381  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::382    if !std.isArray(value) then383      error 'manifestYamlStream only takes arrays, got ' + std.type(value)384    else385      '---\n' + std.join(386        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]387      ) + if c_document_end then '\n...\n' else '\n',388389390  manifestPython(v)::391    if std.isObject(v) then392      local fields = [393        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]394        for k in std.objectFields(v)395      ];396      '{%s}' % [std.join(', ', fields)]397    else if std.isArray(v) then398      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]399    else if std.isString(v) then400      '%s' % [std.escapeStringPython(v)]401    else if std.isFunction(v) then402      error 'cannot manifest function'403    else if std.isNumber(v) then404      std.toString(v)405    else if v == true then406      'True'407    else if v == false then408      'False'409    else if v == null then410      'None',411412  manifestPythonVars(conf)::413    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];414    std.join('\n', vars + ['']),415416  manifestXmlJsonml(value)::417    if !std.isArray(value) then418      error 'Expected a JSONML value (an array), got %s' % std.type(value)419    else420      local aux(v) =421        if std.isString(v) then422          v423        else424          local tag = v[0];425          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);426          local attrs = if has_attrs then v[1] else {};427          local children = if has_attrs then v[2:] else v[1:];428          local attrs_str =429            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);430          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);431432      aux(value),433434  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',435  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },436437  base64:: $intrinsic(base64),438439  base64DecodeBytes:: $intrinsic(base64DecodeBytes),440441  base64Decode:: $intrinsic(base64Decode),442443  reverse:: $intrinsic(reverse),444445  sort:: $intrinsic(sort),446447  uniq(arr, keyF=id)::448    local f(a, b) =449      if std.length(a) == 0 then450        [b]451      else if keyF(a[std.length(a) - 1]) == keyF(b) then452        a453      else454        a + [b];455    std.foldl(f, arr, []),456457  set(arr, keyF=id)::458    std.uniq(std.sort(arr, keyF), keyF),459460  setMember(x, arr, keyF=id)::461    // TODO(dcunnin): Binary chop for O(log n) complexity462    std.length(std.setInter([x], arr, keyF)) > 0,463464  setUnion(a, b, keyF=id)::465    // NOTE: order matters, values in `a` win466    local aux(a, b, i, j, acc) =467      if i >= std.length(a) then468        acc + b[j:]469      else if j >= std.length(b) then470        acc + a[i:]471      else472        local ak = keyF(a[i]);473        local bk = keyF(b[j]);474        if ak == bk then475          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict476        else if ak < bk then477          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict478        else479          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;480    aux(a, b, 0, 0, []),481482  setInter(a, b, keyF=id)::483    local aux(a, b, i, j, acc) =484      if i >= std.length(a) || j >= std.length(b) then485        acc486      else487        if keyF(a[i]) == keyF(b[j]) then488          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict489        else if keyF(a[i]) < keyF(b[j]) then490          aux(a, b, i + 1, j, acc) tailstrict491        else492          aux(a, b, i, j + 1, acc) tailstrict;493    aux(a, b, 0, 0, []) tailstrict,494495  setDiff(a, b, keyF=id)::496    local aux(a, b, i, j, acc) =497      if i >= std.length(a) then498        acc499      else if j >= std.length(b) then500        acc + a[i:]501      else502        if keyF(a[i]) == keyF(b[j]) then503          aux(a, b, i + 1, j + 1, acc) tailstrict504        else if keyF(a[i]) < keyF(b[j]) then505          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict506        else507          aux(a, b, i, j + 1, acc) tailstrict;508    aux(a, b, 0, 0, []) tailstrict,509510  mergePatch(target, patch)::511    if std.isObject(patch) then512      local target_object =513        if std.isObject(target) then target else {};514515      local target_fields =516        if std.isObject(target_object) then std.objectFields(target_object) else [];517518      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];519      local both_fields = std.setUnion(target_fields, std.objectFields(patch));520521      {522        [k]:523          if !std.objectHas(patch, k) then524            target_object[k]525          else if !std.objectHas(target_object, k) then526            std.mergePatch(null, patch[k]) tailstrict527          else528            std.mergePatch(target_object[k], patch[k]) tailstrict529        for k in std.setDiff(both_fields, null_fields)530      }531    else532      patch,533534  get(o, f, default = null, inc_hidden = true)::535    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,536537  objectFields(o)::538    std.objectFieldsEx(o, false),539540  objectFieldsAll(o)::541    std.objectFieldsEx(o, true),542543  objectHas(o, f)::544    std.objectHasEx(o, f, false),545546  objectHasAll(o, f)::547    std.objectHasEx(o, f, true),548549  objectValues(o)::550    [o[k] for k in std.objectFields(o)],551552  objectValuesAll(o)::553    [o[k] for k in std.objectFieldsAll(o)],554555  equals:: $intrinsic(equals),556557  resolvePath(f, r)::558    local arr = std.split(f, '/');559    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),560561  prune(a)::562    local isContent(b) =563      if b == null then564        false565      else if std.isArray(b) then566        std.length(b) > 0567      else if std.isObject(b) then568        std.length(b) > 0569      else570        true;571    if std.isArray(a) then572      [std.prune(x) for x in a if isContent($.prune(x))]573    else if std.isObject(a) then {574      [x]: $.prune(a[x])575      for x in std.objectFields(a)576      if isContent(std.prune(a[x]))577    } else578      a,579580  findSubstr(pat, str)::581    if !std.isString(pat) then582      error 'findSubstr first parameter should be a string, got ' + std.type(pat)583    else if !std.isString(str) then584      error 'findSubstr second parameter should be a string, got ' + std.type(str)585    else586      local pat_len = std.length(pat);587      local str_len = std.length(str);588      if pat_len == 0 || str_len == 0 || pat_len > str_len then589        []590      else591        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),592593  find(value, arr)::594    if !std.isArray(arr) then595      error 'find second parameter should be an array, got ' + std.type(arr)596    else597      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),598}
after · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Magic legacy field6  thisFile:: $intrinsicThisFile,78  # Those functions aren't normally located in stdlib9  length:: $intrinsic(length),10  type:: $intrinsic(type),11  makeArray:: $intrinsic(makeArray),12  codepoint:: $intrinsic(codepoint),13  objectFieldsEx:: $intrinsic(objectFieldsEx),14  objectHasEx:: $intrinsic(objectHasEx),15  primitiveEquals:: $intrinsic(primitiveEquals),16  modulo:: $intrinsic(modulo),17  floor:: $intrinsic(floor),18  ceil:: $intrinsic(ceil),19  extVar:: $intrinsic(extVar),20  native:: $intrinsic(native),21  filter:: $intrinsic(filter),22  char:: $intrinsic(char),23  encodeUTF8:: $intrinsic(encodeUTF8),24  decodeUTF8:: $intrinsic(decodeUTF8),25  md5:: $intrinsic(md5),26  trace:: $intrinsic(trace),27  id:: $intrinsic(id),28  parseJson:: $intrinsic(parseJson),29  parseYaml:: $intrinsic(parseYaml),3031  log:: $intrinsic(log),32  pow:: $intrinsic(pow),33  sqrt:: $intrinsic(sqrt),3435  sin:: $intrinsic(sin),36  cos:: $intrinsic(cos),37  tan:: $intrinsic(tan),38  asin:: $intrinsic(asin),39  acos:: $intrinsic(acos),40  atan:: $intrinsic(atan),4142  exp:: $intrinsic(exp),43  mantissa:: $intrinsic(mantissa),44  exponent:: $intrinsic(exponent),4546  any:: $intrinsic(any),47  all:: $intrinsic(all),4849  isString(v):: std.type(v) == 'string',50  isNumber(v):: std.type(v) == 'number',51  isBoolean(v):: std.type(v) == 'boolean',52  isObject(v):: std.type(v) == 'object',53  isArray(v):: std.type(v) == 'array',54  isFunction(v):: std.type(v) == 'function',5556  toString(a)::57    if std.type(a) == 'string' then a else '' + a,5859  substr:: $intrinsic(substr),6061  startsWith(a, b)::62    if std.length(a) < std.length(b) then63      false64    else65      std.substr(a, 0, std.length(b)) == b,6667  endsWith(a, b)::68    if std.length(a) < std.length(b) then69      false70    else71      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,7273  lstripChars(str, chars)::74    if std.length(str) > 0 && std.member(chars, str[0]) then75      std.lstripChars(str[1:], chars)76    else77      str,7879  rstripChars(str, chars)::80    local len = std.length(str);81    if len > 0 && std.member(chars, str[len - 1]) then82      std.rstripChars(str[:len - 1], chars)83    else84      str,8586  stripChars(str, chars)::87    std.lstripChars(std.rstripChars(str, chars), chars),8889  stringChars(str)::90    std.makeArray(std.length(str), function(i) str[i]),9192  local parse_nat(str, base) =93    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;94    // These codepoints are in ascending order:95    local zero_code = std.codepoint('0');96    local upper_a_code = std.codepoint('A');97    local lower_a_code = std.codepoint('a');98    local addDigit(aggregate, char) =99      local code = std.codepoint(char);100      local digit = if code >= lower_a_code then101        code - lower_a_code + 10102      else if code >= upper_a_code then103        code - upper_a_code + 10104      else105        code - zero_code;106      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];107      base * aggregate + digit;108    std.foldl(addDigit, std.stringChars(str), 0),109110  parseInt(str)::111    assert std.isString(str) : 'Expected string, got ' + std.type(str);112    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];113    if str[0] == '-' then114      -parse_nat(str[1:], 10)115    else116      parse_nat(str, 10),117118  parseOctal(str)::119    assert std.isString(str) : 'Expected string, got ' + std.type(str);120    assert std.length(str) > 0 : 'Not an octal number: ""';121    parse_nat(str, 8),122123  parseHex(str)::124    assert std.isString(str) : 'Expected string, got ' + std.type(str);125    assert std.length(str) > 0 : 'Not hexadecimal: ""';126    parse_nat(str, 16),127128  split(str, c):: std.splitLimit(str, c, -1),129130  splitLimit:: $intrinsic(splitLimit),131132  strReplace:: $intrinsic(strReplace),133134  asciiUpper:: $intrinsic(asciiUpper),135136  asciiLower:: $intrinsic(asciiLower),137138  range:: $intrinsic(range),139140  repeat(what, count)::141    local joiner =142      if std.isString(what) then ''143      else if std.isArray(what) then []144      else error 'std.repeat first argument must be an array or a string';145    std.join(joiner, std.makeArray(count, function(i) what)),146147  slice:: $intrinsic(slice),148149  member:: $intrinsic(member),150151  count:: $intrinsic(count),152153  mod:: $intrinsic(mod),154155  map:: $intrinsic(map),156157  mapWithIndex(func, arr)::158    if !std.isFunction(func) then159      error ('std.mapWithIndex first param must be function, got ' + std.type(func))160    else if !std.isArray(arr) && !std.isString(arr) then161      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))162    else163      std.makeArray(std.length(arr), function(i) func(i, arr[i])),164165  mapWithKey(func, obj)::166    if !std.isFunction(func) then167      error ('std.mapWithKey first param must be function, got ' + std.type(func))168    else if !std.isObject(obj) then169      error ('std.mapWithKey second param must be object, got ' + std.type(obj))170    else171      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },172173  flatMap:: $intrinsic(flatMap),174175  join:: $intrinsic(join),176177  lines(arr)::178    std.join('\n', arr + ['']),179180  deepJoin(arr)::181    if std.isString(arr) then182      arr183    else if std.isArray(arr) then184      std.join('', [std.deepJoin(x) for x in arr])185    else186      error 'Expected string or array, got %s' % std.type(arr),187188189  format:: $intrinsic(format),190191  foldr:: $intrinsic(foldr),192193  foldl:: $intrinsic(foldl),194195  filterMap(filter_func, map_func, arr)::196    if !std.isFunction(filter_func) then197      error ('std.filterMap first param must be function, got ' + std.type(filter_func))198    else if !std.isFunction(map_func) then199      error ('std.filterMap second param must be function, got ' + std.type(map_func))200    else if !std.isArray(arr) then201      error ('std.filterMap third param must be array, got ' + std.type(arr))202    else203      std.map(map_func, std.filter(filter_func, arr)),204205  assertEqual(a, b)::206    if a == b then207      true208    else209      error 'Assertion failed. ' + a + ' != ' + b,210211  abs(n)::212    if !std.isNumber(n) then213      error 'std.abs expected number, got ' + std.type(n)214    else215      if n > 0 then n else -n,216217  sign(n)::218    if !std.isNumber(n) then219      error 'std.sign expected number, got ' + std.type(n)220    else221      if n > 0 then222        1223      else if n < 0 then224        -1225      else 0,226227  max(a, b)::228    if !std.isNumber(a) then229      error 'std.max first param expected number, got ' + std.type(a)230    else if !std.isNumber(b) then231      error 'std.max second param expected number, got ' + std.type(b)232    else233      if a > b then a else b,234235  min(a, b)::236    if !std.isNumber(a) then237      error 'std.min first param expected number, got ' + std.type(a)238    else if !std.isNumber(b) then239      error 'std.min second param expected number, got ' + std.type(b)240    else241      if a < b then a else b,242243  clamp(x, minVal, maxVal)::244    if x < minVal then minVal245    else if x > maxVal then maxVal246    else x,247248  flattenArrays(arrs)::249    std.foldl(function(a, b) a + b, arrs, []),250251  manifestIni(ini)::252    local body_lines(body) =253      std.join([], [254        local value_or_values = body[k];255        if std.isArray(value_or_values) then256          ['%s = %s' % [k, value] for value in value_or_values]257        else258          ['%s = %s' % [k, value_or_values]]259260        for k in std.objectFields(body)261      ]);262263    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),264          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],265          all_sections = [266      section_lines(k, ini.sections[k])267      for k in std.objectFields(ini.sections)268    ];269    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),270271  manifestToml(value):: std.manifestTomlEx(value, '  '),272273  manifestTomlEx(value, indent)::274    local275      escapeStringToml = std.escapeStringJson,276      escapeKeyToml(key) =277        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));278        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),279      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),280      isSection(v) = std.isObject(v) || isTableArray(v),281      renderValue(v, indexedPath, inline, cindent) =282        if v == true then283          'true'284        else if v == false then285          'false'286        else if v == null then287          error 'Tried to manifest "null" at ' + indexedPath288        else if std.isNumber(v) then289          '' + v290        else if std.isString(v) then291          escapeStringToml(v)292        else if std.isFunction(v) then293          error 'Tried to manifest function at ' + indexedPath294        else if std.isArray(v) then295          if std.length(v) == 0 then296            '[]'297          else298            local range = std.range(0, std.length(v) - 1);299            local new_indent = if inline then '' else cindent + indent;300            local separator = if inline then ' ' else '\n';301            local lines = ['[' + separator]302                          + std.join([',' + separator],303                                     [304                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]305                                       for i in range306                                     ])307                          + [separator + (if inline then '' else cindent) + ']'];308            std.join('', lines)309        else if std.isObject(v) then310          local lines = ['{ ']311                        + std.join([', '],312                                   [313                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]314                                     for k in std.objectFields(v)315                                   ])316                        + [' }'];317          std.join('', lines),318      renderTableInternal(v, path, indexedPath, cindent) =319        local kvp = std.flattenArrays([320          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]321          for k in std.objectFields(v)322          if !isSection(v[k])323        ]);324        local sections = [std.join('\n', kvp)] + [325          (326            if std.isObject(v[k]) then327              renderTable(v[k], path + [k], indexedPath + [k], cindent)328            else329              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)330          )331          for k in std.objectFields(v)332          if isSection(v[k])333        ];334        std.join('\n\n', sections),335      renderTable(v, path, indexedPath, cindent) =336        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'337        + (if v == {} then '' else '\n')338        + renderTableInternal(v, path, indexedPath, cindent + indent),339      renderTableArray(v, path, indexedPath, cindent) =340        local range = std.range(0, std.length(v) - 1);341        local sections = [342          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'343           + (if v[i] == {} then '' else '\n')344           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))345          for i in range346        ];347        std.join('\n\n', sections);348    if std.isObject(value) then349      renderTableInternal(value, [], [], '')350    else351      error 'TOML body must be an object. Got ' + std.type(value),352353  escapeStringJson:: $intrinsic(escapeStringJson),354355  escapeStringPython(str)::356    std.escapeStringJson(str),357358  escapeStringBash(str_)::359    local str = std.toString(str_);360    local trans(ch) =361      if ch == "'" then362        "'\"'\"'"363      else364        ch;365    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),366367  escapeStringDollars(str_)::368    local str = std.toString(str_);369    local trans(ch) =370      if ch == '$' then371        '$$'372      else373        ch;374    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),375376  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,377378  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),379380  manifestJsonEx:: $intrinsic(manifestJsonEx),381382  manifestYamlDoc:: $intrinsic(manifestYamlDoc),383384  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::385    if !std.isArray(value) then386      error 'manifestYamlStream only takes arrays, got ' + std.type(value)387    else388      '---\n' + std.join(389        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]390      ) + if c_document_end then '\n...\n' else '\n',391392393  manifestPython(v)::394    if std.isObject(v) then395      local fields = [396        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]397        for k in std.objectFields(v)398      ];399      '{%s}' % [std.join(', ', fields)]400    else if std.isArray(v) then401      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]402    else if std.isString(v) then403      '%s' % [std.escapeStringPython(v)]404    else if std.isFunction(v) then405      error 'cannot manifest function'406    else if std.isNumber(v) then407      std.toString(v)408    else if v == true then409      'True'410    else if v == false then411      'False'412    else if v == null then413      'None',414415  manifestPythonVars(conf)::416    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];417    std.join('\n', vars + ['']),418419  manifestXmlJsonml(value)::420    if !std.isArray(value) then421      error 'Expected a JSONML value (an array), got %s' % std.type(value)422    else423      local aux(v) =424        if std.isString(v) then425          v426        else427          local tag = v[0];428          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);429          local attrs = if has_attrs then v[1] else {};430          local children = if has_attrs then v[2:] else v[1:];431          local attrs_str =432            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);433          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);434435      aux(value),436437  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',438  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },439440  base64:: $intrinsic(base64),441442  base64DecodeBytes:: $intrinsic(base64DecodeBytes),443444  base64Decode:: $intrinsic(base64Decode),445446  reverse:: $intrinsic(reverse),447448  sort:: $intrinsic(sort),449450  uniq(arr, keyF=id)::451    local f(a, b) =452      if std.length(a) == 0 then453        [b]454      else if keyF(a[std.length(a) - 1]) == keyF(b) then455        a456      else457        a + [b];458    std.foldl(f, arr, []),459460  set(arr, keyF=id)::461    std.uniq(std.sort(arr, keyF), keyF),462463  setMember(x, arr, keyF=id)::464    // TODO(dcunnin): Binary chop for O(log n) complexity465    std.length(std.setInter([x], arr, keyF)) > 0,466467  setUnion(a, b, keyF=id)::468    // NOTE: order matters, values in `a` win469    local aux(a, b, i, j, acc) =470      if i >= std.length(a) then471        acc + b[j:]472      else if j >= std.length(b) then473        acc + a[i:]474      else475        local ak = keyF(a[i]);476        local bk = keyF(b[j]);477        if ak == bk then478          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict479        else if ak < bk then480          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict481        else482          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;483    aux(a, b, 0, 0, []),484485  setInter(a, b, keyF=id)::486    local aux(a, b, i, j, acc) =487      if i >= std.length(a) || j >= std.length(b) then488        acc489      else490        if keyF(a[i]) == keyF(b[j]) then491          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict492        else if keyF(a[i]) < keyF(b[j]) then493          aux(a, b, i + 1, j, acc) tailstrict494        else495          aux(a, b, i, j + 1, acc) tailstrict;496    aux(a, b, 0, 0, []) tailstrict,497498  setDiff(a, b, keyF=id)::499    local aux(a, b, i, j, acc) =500      if i >= std.length(a) then501        acc502      else if j >= std.length(b) then503        acc + a[i:]504      else505        if keyF(a[i]) == keyF(b[j]) then506          aux(a, b, i + 1, j + 1, acc) tailstrict507        else if keyF(a[i]) < keyF(b[j]) then508          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict509        else510          aux(a, b, i, j + 1, acc) tailstrict;511    aux(a, b, 0, 0, []) tailstrict,512513  mergePatch(target, patch)::514    if std.isObject(patch) then515      local target_object =516        if std.isObject(target) then target else {};517518      local target_fields =519        if std.isObject(target_object) then std.objectFields(target_object) else [];520521      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];522      local both_fields = std.setUnion(target_fields, std.objectFields(patch));523524      {525        [k]:526          if !std.objectHas(patch, k) then527            target_object[k]528          else if !std.objectHas(target_object, k) then529            std.mergePatch(null, patch[k]) tailstrict530          else531            std.mergePatch(target_object[k], patch[k]) tailstrict532        for k in std.setDiff(both_fields, null_fields)533      }534    else535      patch,536537  get(o, f, default = null, inc_hidden = true)::538    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,539540  objectFields(o)::541    std.objectFieldsEx(o, false),542543  objectFieldsAll(o)::544    std.objectFieldsEx(o, true),545546  objectHas(o, f)::547    std.objectHasEx(o, f, false),548549  objectHasAll(o, f)::550    std.objectHasEx(o, f, true),551552  objectValues(o)::553    [o[k] for k in std.objectFields(o)],554555  objectValuesAll(o)::556    [o[k] for k in std.objectFieldsAll(o)],557558  equals:: $intrinsic(equals),559560  resolvePath(f, r)::561    local arr = std.split(f, '/');562    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),563564  prune(a)::565    local isContent(b) =566      if b == null then567        false568      else if std.isArray(b) then569        std.length(b) > 0570      else if std.isObject(b) then571        std.length(b) > 0572      else573        true;574    if std.isArray(a) then575      [std.prune(x) for x in a if isContent($.prune(x))]576    else if std.isObject(a) then {577      [x]: $.prune(a[x])578      for x in std.objectFields(a)579      if isContent(std.prune(a[x]))580    } else581      a,582583  findSubstr(pat, str)::584    if !std.isString(pat) then585      error 'findSubstr first parameter should be a string, got ' + std.type(pat)586    else if !std.isString(str) then587      error 'findSubstr second parameter should be a string, got ' + std.type(str)588    else589      local pat_len = std.length(pat);590      local str_len = std.length(str);591      if pat_len == 0 || str_len == 0 || pat_len > str_len then592        []593      else594        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),595596  find(value, arr)::597    if !std.isArray(arr) then598      error 'find second parameter should be an array, got ' + std.type(arr)599    else600      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),601}