Skip to content

Commit 1b0d0aa

Browse files
committed
Add C++ API.
Fixes #71
1 parent 32490bf commit 1b0d0aa

7 files changed

+451
-14
lines changed

BUILD

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,25 @@ genrule(
1717
# TODO(dzc): Remove the includes = ["."] lines from all cc_* targets once
1818
# bazelbuild/bazel#445 is fixed.
1919
cc_library(
20-
name = "jsonnet-common",
20+
name = "libjsonnet",
2121
srcs = [
2222
"core/desugaring.cpp",
23+
"core/libjsonnet.cpp",
2324
"core/lexer.cpp",
2425
"core/parser.cpp",
2526
"core/static_analysis.cpp",
2627
"core/vm.cpp",
27-
"stdlib/std.jsonnet.h",
28-
],
29-
hdrs = [
30-
"core/desugaring.h",
31-
"core/lexer.h",
32-
"core/parser.h",
33-
"core/static_analysis.h",
34-
"core/static_error.h",
35-
"core/vm.h",
3628
],
29+
hdrs = ["core/libjsonnet.h"],
3730
includes = ["."],
31+
linkopts = ["-lm"],
3832
)
3933

4034
cc_library(
41-
name = "libjsonnet",
42-
srcs = ["core/libjsonnet.cpp"],
43-
hdrs = ["core/libjsonnet.h"],
44-
deps = [":jsonnet-common"],
35+
name = "jsonnet-cpp",
36+
srcs = ["cpp/jsonnet.cc"],
37+
hdrs = ["cpp/jsonnet.h"],
38+
deps = [":libjsonnet"],
4539
includes = ["."],
4640
)
4741

@@ -81,3 +75,25 @@ sh_test(
8175
":object_jsonnet",
8276
],
8377
)
78+
79+
cc_binary(
80+
name = "libjsonnet_cpp_test_snippet",
81+
srcs = ["cpp/libjsonnet_cpp_test_snippet.cc"],
82+
deps = [":jsonnet-cpp"],
83+
)
84+
85+
cc_binary(
86+
name = "libjsonnet_cpp_test_file",
87+
srcs = ["cpp/libjsonnet_cpp_test_file.cc"],
88+
deps = [":jsonnet-cpp"],
89+
)
90+
91+
sh_test(
92+
name = "libjsonnet_cpp_test",
93+
srcs = ["cpp/libjsonnet_cpp_test.sh"],
94+
data = [
95+
":libjsonnet_cpp_test_snippet",
96+
":libjsonnet_cpp_test_file",
97+
":object_jsonnet",
98+
],
99+
)

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,25 @@ LIB_SRC = \
4848
core/parser.cpp \
4949
core/static_analysis.cpp \
5050
core/vm.cpp
51+
5152
LIB_OBJ = $(LIB_SRC:.cpp=.o)
5253

54+
LIB_CPP_SRC = \
55+
$(LIB_SRC) \
56+
cpp/jsonnet.cc
57+
58+
LIB_CPP_OBJ = $(LIB_CPP_SRC:.cpp=.o)
59+
5360
ALL = \
5461
jsonnet \
5562
libjsonnet.so \
63+
libjsonnet-cpp.so \
5664
libjsonnet_test_snippet \
5765
libjsonnet_test_file \
5866
libjsonnet.js \
5967
doc/libjsonnet.js \
6068
$(LIB_OBJ)
69+
6170
ALL_HEADERS = \
6271
core/ast.h \
6372
core/desugaring.h \
@@ -68,6 +77,7 @@ ALL_HEADERS = \
6877
core/static_analysis.h \
6978
core/static_error.h \
7079
core/vm.h \
80+
cpp/jsonnet.h \
7181
stdlib/std.jsonnet.h
7282

7383
default: jsonnet
@@ -105,6 +115,9 @@ jsonnet: cmd/jsonnet.cpp $(LIB_OBJ)
105115
libjsonnet.so: $(LIB_OBJ)
106116
$(CXX) $(LDFLAGS) $(LIB_OBJ) $(SHARED_LDFLAGS) -o $@
107117

118+
libjsonnet-cpp.so: $(LIB_CPP_OBJ)
119+
$(CXX) $(LDFLAGS) $(LIB_CPP_OBJ) $(SHARED_LDFLAGS) -o $@
120+
108121
# Javascript build of C binding
109122
JS_EXPORTED_FUNCTIONS = 'EXPORTED_FUNCTIONS=["_jsonnet_make", "_jsonnet_evaluate_snippet", "_jsonnet_realloc", "_jsonnet_destroy"]'
110123

cpp/jsonnet.cc

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 "cpp/jsonnet.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+
output->assign(jsonnet_output);
72+
return error == 0;
73+
}
74+
75+
bool Jsonnet::EvaluateSnippet(const std::string& filename,
76+
const std::string& snippet,
77+
std::string* output) {
78+
if (output == nullptr) {
79+
return false;
80+
}
81+
int error = 0;
82+
const char* jsonnet_output = ::jsonnet_evaluate_snippet(
83+
vm_, filename.c_str(), snippet.c_str(), &error);
84+
output->assign(jsonnet_output);
85+
return error == 0;
86+
}
87+
88+
namespace {
89+
void ParseMultiOutput(const char* jsonnet_output,
90+
std::map<std::string, std::string>* outputs) {
91+
for (const char* c = jsonnet_output; *c != '\0'; ) {
92+
const char *filename = c;
93+
const char *c2 = c;
94+
while (*c2 != '\0') ++c2;
95+
++c2;
96+
const char *json = c2;
97+
while (*c2 != '\0') ++c2;
98+
++c2;
99+
c = c2;
100+
outputs->insert(std::make_pair(filename, json));
101+
}
102+
}
103+
} // namespace
104+
105+
bool Jsonnet::EvaluateFileMulti(
106+
const std::string& filename,
107+
std::map<std::string, std::string>* outputs) {
108+
if (outputs == nullptr) {
109+
return false;
110+
}
111+
int error = 0;
112+
const char* jsonnet_output =
113+
::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error);
114+
ParseMultiOutput(jsonnet_output, outputs);
115+
return error == 0;
116+
}
117+
118+
bool Jsonnet::EvaluateSnippetMulti(
119+
const std::string& filename,
120+
const std::string& snippet,
121+
std::map<std::string, std::string>* outputs) {
122+
if (outputs == nullptr) {
123+
return false;
124+
}
125+
int error = 0;
126+
const char* jsonnet_output = ::jsonnet_evaluate_snippet_multi(
127+
vm_, filename.c_str(), snippet.c_str(), &error);
128+
ParseMultiOutput(jsonnet_output, outputs);
129+
return error == 0;
130+
}
131+
132+
} // namespace jsonnet

0 commit comments

Comments
 (0)