Skip to content

Commit ff35df9

Browse files
committed
Fix #15
1 parent d011c6f commit ff35df9

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

parser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ namespace {
732732
};
733733
}
734734

735-
static unsigned long max_builtin = 15;
735+
static unsigned long max_builtin = 17;
736736
BuiltinDecl jsonnet_builtin_decl(unsigned long builtin)
737737
{
738738
switch (builtin) {
@@ -752,6 +752,8 @@ BuiltinDecl jsonnet_builtin_decl(unsigned long builtin)
752752
case 13: return {"objectHas", {"obj", "f"}};
753753
case 14: return {"length", {"x"}};
754754
case 15: return {"objectFields", {"obj"}};
755+
case 16: return {"codepoint", {"str"}};
756+
case 17: return {"char", {"n"}};
755757
default:
756758
std::cerr << "INTERNAL ERROR: Unrecognized builtin function: " << builtin << std::endl;
757759
std::abort();

test_suite/std.jsonnet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ std.assertEqual(std.toString([1,2,"foo"]), "[1, 2, \"foo\"]") &&
8888
std.assertEqual(std.substr("cookie", 1, 3), "ook") &&
8989
std.assertEqual(std.substr("cookie", 1, 0), "") &&
9090

91+
std.assertEqual(std.codepoint("a"), 97) &&
92+
std.assertEqual(std.char(97), "a") &&
93+
9194
std.assertEqual(std.map(function(x)x*x, []), []) &&
9295
std.assertEqual(std.map(function(x)x*x, [1,2,3,4]), [1,4,9,16]) &&
9396
std.assertEqual(std.map(function(x) x*x, std.filter(function(x)x>5, std.range(1,10))), [36, 49, 64, 81, 100]) &&

vm.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,34 @@ namespace {
15741574
}
15751575
} break;
15761576

1577+
case 16: { // codepoint
1578+
validateBuiltinArgs(loc, builtin, args, {Value::STRING});
1579+
if (static_cast<HeapString*>(args[0].v.h)->value.length() != 1)
1580+
throw makeError(loc,
1581+
"codepoint takes a string of length 1, "
1582+
"got " + type_str(args[0]));
1583+
char c = static_cast<HeapString*>(args[0].v.h)->value[0];
1584+
scratch = makeDouble((unsigned char)(c));
1585+
} break;
1586+
1587+
case 17: { // char
1588+
validateBuiltinArgs(loc, builtin, args, {Value::DOUBLE});
1589+
long l = (unsigned long)(args[0].v.d);
1590+
if (l < 0) {
1591+
std::stringstream ss;
1592+
ss << "Codepoints must be >= 0, got " << l;
1593+
throw makeError(ast.location, ss.str());
1594+
}
1595+
if (l >= 128) {
1596+
std::stringstream ss;
1597+
ss << "Sorry, only ASCII supported right now. ";
1598+
ss << "Codepoints must be < 128, got " << l;
1599+
throw makeError(ast.location, ss.str());
1600+
}
1601+
char c = l;
1602+
scratch = makeString(std::string(&c, 1));
1603+
} break;
1604+
15771605
default:
15781606
std::cerr << "INTERNAL ERROR: Unrecognized builtin: " << builtin
15791607
<< std::endl;

0 commit comments

Comments
 (0)