Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust-python-parser tidy up #3503

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/rust/python-parser/BUILD
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
load("//:build/kj_test.bzl", "kj_test")
load("//:build/wd_cc_library.bzl", "wd_cc_library")
load("//:build/wd_rust_crate.bzl", "rust_cxx_include", "wd_rust_crate")

wd_cc_library(
name = "cxx-bridge",
srcs = [
"cxx-bridge.c++",
],
hdrs = [
"cxx-bridge.h",
],
linkstatic = True,
deps = [
"//src/rust/cxx-integration",
"@capnp-cpp//src/kj",
],
)

wd_rust_crate(
name = "python-parser",
cxx_bridge_src = "lib.rs",
visibility = ["//visibility:public"],
deps = [
":cxx-bridge",
"@crates_vendor//:ruff_python_ast",
"@crates_vendor//:ruff_python_parser",
],
)

kj_test(
src = "import_parsing.c++",
src = "import-parsing-test.c++",
deps = [
":python-parser",
"//deps/rust:runtime",
Expand Down
26 changes: 26 additions & 0 deletions src/rust/python-parser/cxx-bridge.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

// #include <workerd/rust/python-parser/lib.rs.h>
#include "workerd/rust/cxx-integration/lib.rs.h"

#include <kj/test.h>

namespace workerd::rust::python_parser {
::rust::Vec<::rust::String> get_imports(::rust::Slice<::rust::Str const> sources);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love declaring this manually, but at least C++ name mangling means there is a very high probability of a link error rather than generating a bad executable if the declaration is wrong. The problem is this is declared in workerd/rust/python-parser/lib.rs.h which is generated by wd_rust_crate but that is a dependent of the rule that compiles cxx-bridge.c++. This probably means I'm doing something wrong in Bazel? cc @mikea

}

using ::workerd::rust::python_parser::get_imports;

namespace workerd::api::pyodide {
kj::Array<kj::String> parseImports(kj::ArrayPtr<kj::StringPtr> cpp_modules) {
auto rust_modules = kj::heapArrayBuilder<::rust::Str const>(cpp_modules.size());
for (auto& entry: cpp_modules) {
rust_modules.add(entry.cStr());
}
::rust::Slice<::rust::Str const> rust_slice(rust_modules.begin(), rust_modules.size());
auto rust_result = get_imports(rust_slice);
return workerd::fromRust(rust_result);
}
}
5 changes: 5 additions & 0 deletions src/rust/python-parser/cxx-bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "kj/string.h"

namespace workerd::api::pyodide {
kj::Array<kj::String> parseImports(kj::ArrayPtr<kj::StringPtr> cpp_modules);
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#include "import_parsing.h"
#include "kj/test.h"

#include <workerd/rust/python-parser/lib.rs.h>
#include "workerd/rust/cxx-integration/lib.rs.h"

#include <kj/test.h>

using ::edgeworker::rust::python_parser::get_imports;

kj::Array<kj::String> parseImports(kj::ArrayPtr<kj::StringPtr> cpp_modules) {
auto rust_modules = kj::heapArrayBuilder<::rust::Str const>(cpp_modules.size());
for (auto& entry: cpp_modules) {
rust_modules.add(entry.cStr());
}
::rust::Slice<::rust::Str const> rust_slice(rust_modules.begin(), rust_modules.size());
auto rust_result = get_imports(rust_slice);
return workerd::fromRust(rust_result);
}

namespace workerd::api {
namespace workerd::api::pyodide {
namespace {

KJ_TEST("basic `import` tests") {
Expand Down
3 changes: 1 addition & 2 deletions src/rust/python-parser/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::collections::HashSet;
use ruff_python_ast::{Stmt, StmtImportFrom};
use ruff_python_parser::parse_module;

#[cxx::bridge(namespace = "edgeworker::rust::python_parser")]
#[cxx::bridge(namespace = "workerd::rust::python_parser")]
mod ffi {

extern "Rust" {
fn get_imports(sources: &[&str]) -> Vec<String>;
}
Expand Down
Loading