Skip to content

Commit 7a93b9f

Browse files
committed
Add C++ API.
Fixes #71
1 parent c46d01d commit 7a93b9f

10 files changed

+584
-2
lines changed

BUILD

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cc_library(
1919
srcs = [
2020
"core/desugarer.cpp",
2121
"core/formatter.cpp",
22+
"core/libjsonnet.cpp",
2223
"core/lexer.cpp",
2324
"core/parser.cpp",
2425
"core/static_analysis.cpp",
@@ -52,6 +53,14 @@ cc_library(
5253
includes = ["include"],
5354
)
5455

56+
cc_library(
57+
name = "libjsonnet++",
58+
srcs = ["cpp/libjsonnet++.cc"],
59+
hdrs = ["include/libjsonnet++.h"],
60+
deps = [":libjsonnet"],
61+
includes = ["include"],
62+
)
63+
5564
cc_binary(
5665
name = "jsonnet",
5766
srcs = ["cmd/jsonnet.cpp"],
@@ -88,3 +97,27 @@ sh_test(
8897
":object_jsonnet",
8998
],
9099
)
100+
101+
cc_binary(
102+
name = "libjsonnet_cpp_test_snippet",
103+
srcs = ["cpp/libjsonnet_cpp_test_snippet.cc"],
104+
deps = [":libjsonnet++"],
105+
includes = ["include"],
106+
)
107+
108+
cc_binary(
109+
name = "libjsonnet_cpp_test_file",
110+
srcs = ["cpp/libjsonnet_cpp_test_file.cc"],
111+
deps = [":libjsonnet++"],
112+
includes = ["include"],
113+
)
114+
115+
sh_test(
116+
name = "libjsonnet_cpp_test",
117+
srcs = ["cpp/libjsonnet_cpp_test.sh"],
118+
data = [
119+
":libjsonnet_cpp_test_snippet",
120+
":libjsonnet_cpp_test_file",
121+
":object_jsonnet",
122+
],
123+
)

Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,24 @@ LIB_SRC = \
5050
core/static_analysis.cpp \
5151
core/string_utils.cpp \
5252
core/vm.cpp
53+
5354
LIB_OBJ = $(LIB_SRC:.cpp=.o)
5455

56+
LIB_CPP_SRC = \
57+
cpp/libjsonnet++.cc
58+
59+
LIB_CPP_OBJ = $(LIB_OBJ) $(LIB_CPP_SRC:.cc=.o)
60+
5561
ALL = \
5662
jsonnet \
5763
libjsonnet.so \
64+
libjsonnet++.so \
5865
libjsonnet_test_snippet \
5966
libjsonnet_test_file \
6067
libjsonnet.js \
6168
doc/js/libjsonnet.js \
6269
$(LIB_OBJ)
70+
6371
ALL_HEADERS = \
6472
core/ast.h \
6573
core/desugarer.h \
@@ -72,7 +80,8 @@ ALL_HEADERS = \
7280
core/string_utils.h \
7381
core/vm.h \
7482
core/std.jsonnet.h \
75-
include/libjsonnet.h
83+
include/libjsonnet.h \
84+
include/libjsonnet++.h
7685

7786
default: jsonnet
7887

@@ -91,7 +100,9 @@ test: jsonnet libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file
91100
MAKEDEPEND_SRCS = \
92101
cmd/jsonnet.cpp \
93102
core/libjsonnet_test_snippet.c \
94-
core/libjsonnet_test_file.c
103+
core/libjsonnet_test_file.c \
104+
cpp/libjsonnet_cpp_test_snippet.c \
105+
cpp/libjsonnet_cpp_test_file.c
95106

96107
depend:
97108
makedepend -f- $(LIB_SRC) $(MAKEDEPEND_SRCS) > Makefile.depend
@@ -102,6 +113,9 @@ core/desugarer.cpp: core/std.jsonnet.h
102113
%.o: %.cpp
103114
$(CXX) -c $(CXXFLAGS) $< -o $@
104115

116+
%.o: %.cc
117+
$(CXX) -c $(CXXFLAGS) $< -o $@
118+
105119
# Commandline executable.
106120
jsonnet: cmd/jsonnet.cpp $(LIB_OBJ)
107121
$(CXX) $(CXXFLAGS) $(LDFLAGS) $< $(LIB_SRC:.cpp=.o) -o $@
@@ -110,6 +124,9 @@ jsonnet: cmd/jsonnet.cpp $(LIB_OBJ)
110124
libjsonnet.so: $(LIB_OBJ)
111125
$(CXX) $(LDFLAGS) $(LIB_OBJ) $(SHARED_LDFLAGS) -o $@
112126

127+
libjsonnet++.so: $(LIB_CPP_OBJ)
128+
$(CXX) $(LDFLAGS) $(LIB_CPP_OBJ) $(SHARED_LDFLAGS) -o $@
129+
113130
# Javascript build of C binding
114131
JS_EXPORTED_FUNCTIONS = 'EXPORTED_FUNCTIONS=["_jsonnet_make", "_jsonnet_evaluate_snippet", "_jsonnet_realloc", "_jsonnet_destroy"]'
115132

WORKSPACE

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
new_http_archive(
2+
name = "gmock_archive",
3+
url = "https://googlemock.googlecode.com/files/gmock-1.7.0.zip",
4+
sha256 = "26fcbb5925b74ad5fc8c26b0495dfc96353f4d553492eb97e85a8a6d2f43095b",
5+
build_file = "gmock.BUILD",
6+
)
7+
8+
bind(
9+
name = "gtest",
10+
actual = "@gmock_archive//:gtest",
11+
)
12+
13+
bind(
14+
name = "gtest_main",
15+
actual = "@gmock_archive//:gtest_main",
16+
)

cpp/libjsonnet++.cc

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2015 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "libjsonnet++.h"
16+
17+
namespace jsonnet {
18+
19+
Jsonnet::Jsonnet() {}
20+
Jsonnet::~Jsonnet() {
21+
if (vm_ != nullptr) {
22+
::jsonnet_destroy(vm_);
23+
}
24+
}
25+
26+
bool Jsonnet::Init() {
27+
vm_ = static_cast<struct JsonnetVm*>(::jsonnet_make());
28+
return vm_ != nullptr;
29+
}
30+
31+
void Jsonnet::SetMaxStack(uint32_t depth) {
32+
::jsonnet_max_stack(vm_, static_cast<unsigned>(depth));
33+
}
34+
35+
void Jsonnet::SetGcMinObjects(uint32_t objects) {
36+
::jsonnet_gc_min_objects(vm_, static_cast<unsigned>(objects));
37+
}
38+
39+
void Jsonnet::SetGcGrowthTrigger(double growth) {
40+
::jsonnet_gc_growth_trigger(vm_, growth);
41+
}
42+
43+
void Jsonnet::SetStringOutput(bool string_output) {
44+
::jsonnet_string_output(vm_, string_output);
45+
}
46+
47+
void Jsonnet::SetDebugAst(bool debug_ast) {
48+
::jsonnet_debug_ast(vm_, static_cast<int>(debug_ast));
49+
}
50+
51+
void Jsonnet::SetMaxTrace(uint32_t lines) {
52+
::jsonnet_max_trace(vm_, static_cast<unsigned>(lines));
53+
}
54+
55+
void Jsonnet::BindExtVar(const std::string& key, const std::string& value) {
56+
::jsonnet_ext_var(vm_, key.c_str(), value.c_str());
57+
}
58+
59+
void Jsonnet::BindExtCodeVar(const std::string& key,
60+
const std::string& value) {
61+
::jsonnet_ext_code(vm_, key.c_str(), value.c_str());
62+
}
63+
64+
bool Jsonnet::EvaluateFile(const std::string& filename, std::string* output) {
65+
if (output == nullptr) {
66+
return false;
67+
}
68+
int error = 0;
69+
const char* jsonnet_output =
70+
::jsonnet_evaluate_file(vm_, filename.c_str(), &error);
71+
if (error != 0) {
72+
last_error_.assign(jsonnet_output);
73+
return false;
74+
}
75+
output->assign(jsonnet_output);
76+
return true;
77+
}
78+
79+
bool Jsonnet::EvaluateSnippet(const std::string& filename,
80+
const std::string& snippet,
81+
std::string* output) {
82+
if (output == nullptr) {
83+
return false;
84+
}
85+
int error = 0;
86+
const char* jsonnet_output = ::jsonnet_evaluate_snippet(
87+
vm_, filename.c_str(), snippet.c_str(), &error);
88+
if (error != 0) {
89+
last_error_.assign(jsonnet_output);
90+
return false;
91+
}
92+
output->assign(jsonnet_output);
93+
return true;
94+
}
95+
96+
namespace {
97+
void ParseMultiOutput(const char* jsonnet_output,
98+
std::map<std::string, std::string>* outputs) {
99+
for (const char* c = jsonnet_output; *c != '\0'; ) {
100+
const char *filename = c;
101+
const char *c2 = c;
102+
while (*c2 != '\0') ++c2;
103+
++c2;
104+
const char *json = c2;
105+
while (*c2 != '\0') ++c2;
106+
++c2;
107+
c = c2;
108+
outputs->insert(std::make_pair(filename, json));
109+
}
110+
}
111+
} // namespace
112+
113+
bool Jsonnet::EvaluateFileMulti(
114+
const std::string& filename,
115+
std::map<std::string, std::string>* outputs) {
116+
if (outputs == nullptr) {
117+
return false;
118+
}
119+
int error = 0;
120+
const char* jsonnet_output =
121+
::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error);
122+
if (error != 0) {
123+
last_error_.assign(jsonnet_output);
124+
return false;
125+
}
126+
ParseMultiOutput(jsonnet_output, outputs);
127+
return true;
128+
}
129+
130+
bool Jsonnet::EvaluateSnippetMulti(
131+
const std::string& filename,
132+
const std::string& snippet,
133+
std::map<std::string, std::string>* outputs) {
134+
if (outputs == nullptr) {
135+
return false;
136+
}
137+
int error = 0;
138+
const char* jsonnet_output = ::jsonnet_evaluate_snippet_multi(
139+
vm_, filename.c_str(), snippet.c_str(), &error);
140+
if (error != 0) {
141+
last_error_.assign(jsonnet_output);
142+
return false;
143+
}
144+
ParseMultiOutput(jsonnet_output, outputs);
145+
return true;
146+
}
147+
148+
std::string Jsonnet::LastError() const {
149+
return last_error_;
150+
}
151+
152+
} // namespace jsonnet

cpp/libjsonnet++_test.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2015 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "libjsonnet++.h"
16+
17+
namespace jsonnet {
18+
19+
struct ImportCallbackContext {
20+
std::string import_path;
21+
}
22+
23+
bool ImportCallback(const std::string& base_dir,
24+
const std::string& path,
25+
ImportCallbackContext* context,
26+
std::string* found_here,
27+
std::string* output) {
28+
29+
}
30+
31+
const char* kJsonnetSnippet = R"(
32+
local foo = import "bar.jsonnet";
33+
34+
{
35+
bar: foo.bar,
36+
}
37+
)";
38+
39+
const char* kExpectedJsonSnippet = R"(
40+
{
41+
"bar": "baz"
42+
}
43+
)";
44+
45+
TEST(ImportTest) {
46+
Jsonnet jsonnet;
47+
ASSERT_TRUE(jsonnet.Init());
48+
49+
ImportCallbackContext context{"cpp/testdata"};
50+
ImportCallbackWrapper callback_wrapper(ImportCallback);
51+
jsonnet.SetImportCallback(&callback_wrapper);
52+
53+
std::string output;
54+
ASSERT_TRUE(jsonnet.EvaluateSnippet("snippet", kJsonnetSnippet, &output))
55+
<< output;
56+
57+
ASSERT_EQ(kExpectedJsonSnippet, output);
58+
}
59+
60+
} // namespace jsonnet

cpp/libjsonnet_cpp_test.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
readonly TEST_SNIPPET="std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)"
16+
readonly LIBJSONNET_CPP_TEST_SNIPPET="libjsonnet_cpp_test_snippet"
17+
readonly LIBJSONNET_CPP_TEST_FILE="libjsonnet_cpp_test_file"
18+
readonly OBJECT_JSONNET="test_suite/object.jsonnet"
19+
20+
function test_libjsonnet_cpp_snippet {
21+
$LIBJSONNET_CPP_TEST_SNIPPET $TEST_SNIPPET
22+
}
23+
24+
function test_libjsonnet_cpp_file {
25+
$LIBJSONNET_CPP_TEST_FILE $OBJECT_JSONNET
26+
}
27+
28+
function main {
29+
test_libjsonnet_cpp_snippet
30+
test_libjsonnet_cpp_file
31+
}
32+
33+
main

0 commit comments

Comments
 (0)