Skip to content
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
40 changes: 24 additions & 16 deletions maldoca/js/ast/transforms/extract_prelude/pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,25 @@ function bar() {

QuickJsBabel babel;

JsSourceRepr source_repr{kSource, std::nullopt};

BabelParseRequest parse_request;
parse_request.set_compute_scopes(true);

MALDOCA_ASSERT_OK_AND_ASSIGN(
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(kSource, parse_request,
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(source_repr, parse_request,
absl::InfiniteDuration(),
std::nullopt, babel));

JsirAnalysisConfig::DynamicConstantPropagation prelude =
ExtractPreludeByIndices(kSource, indices, *repr.ast);

MALDOCA_ASSERT_OK_AND_ASSIGN(
JsSourceRepr source_repr,
ToJsSourceRepr::FromJsAstRepr(*repr.ast, {}, absl::InfiniteDuration(),
JsSourceRepr printed_source_repr,
ToJsSourceRepr::FromJsAstRepr(repr, {}, absl::InfiniteDuration(),
babel));

EXPECT_EQ(source_repr.source, kExpectedSourceWithoutPrelude);
EXPECT_EQ(printed_source_repr.source, kExpectedSourceWithoutPrelude);

EXPECT_EQ(prelude.prelude_source(), kExpectedPrelude);
EXPECT_EQ(prelude.extracted_from_scope_uid(), 0);
Expand All @@ -103,23 +105,25 @@ function bar() {

QuickJsBabel babel;

JsSourceRepr source_repr{kSource, std::nullopt};

BabelParseRequest parse_request;
parse_request.set_compute_scopes(true);

MALDOCA_ASSERT_OK_AND_ASSIGN(
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(kSource, parse_request,
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(source_repr, parse_request,
absl::InfiniteDuration(),
std::nullopt, babel));

JsirAnalysisConfig::DynamicConstantPropagation prelude =
ExtractPreludeByAnnotations(kSource, *repr.ast);

MALDOCA_ASSERT_OK_AND_ASSIGN(
JsSourceRepr source_repr,
ToJsSourceRepr::FromJsAstRepr(*repr.ast, {}, absl::InfiniteDuration(),
JsSourceRepr printed_source_repr,
ToJsSourceRepr::FromJsAstRepr(repr, {}, absl::InfiniteDuration(),
babel));

EXPECT_EQ(source_repr.source, kExpectedSourceWithoutPrelude);
EXPECT_EQ(printed_source_repr.source, kExpectedSourceWithoutPrelude);

EXPECT_EQ(prelude.prelude_source(), kExpectedPrelude);
EXPECT_EQ(prelude.extracted_from_scope_uid(), 0);
Expand All @@ -141,23 +145,25 @@ function bar() {

QuickJsBabel babel;

JsSourceRepr source_repr{kSource, std::nullopt};

BabelParseRequest parse_request;
parse_request.set_compute_scopes(true);

MALDOCA_ASSERT_OK_AND_ASSIGN(
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(kSource, parse_request,
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(source_repr, parse_request,
absl::InfiniteDuration(),
std::nullopt, babel));

JsirAnalysisConfig::DynamicConstantPropagation prelude =
ExtractPreludeByIndicesAndAnnotations(kSource, indices, *repr.ast);

MALDOCA_ASSERT_OK_AND_ASSIGN(
JsSourceRepr source_repr,
ToJsSourceRepr::FromJsAstRepr(*repr.ast, {}, absl::InfiniteDuration(),
JsSourceRepr printed_source_repr,
ToJsSourceRepr::FromJsAstRepr(repr, {}, absl::InfiniteDuration(),
babel));

EXPECT_EQ(source_repr.source, kExpectedSourceWithoutPrelude);
EXPECT_EQ(printed_source_repr.source, kExpectedSourceWithoutPrelude);

EXPECT_EQ(prelude.prelude_source(), kExpectedPrelude);
EXPECT_EQ(prelude.extracted_from_scope_uid(), 0);
Expand All @@ -171,20 +177,22 @@ TEST(ExtractPreludePassTest, ReuseBabel) {

QuickJsBabel babel;

JsSourceRepr source_repr{kSource, std::nullopt};

BabelParseRequest parse_request;
parse_request.set_compute_scopes(true);

// Parse the source code once so that Babel increments the scope uid counter.
{
MALDOCA_ASSERT_OK_AND_ASSIGN(
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(kSource, parse_request,
absl::InfiniteDuration(),
std::nullopt, babel));
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(
source_repr, parse_request,
absl::InfiniteDuration(), std::nullopt, babel));
}

// This time the global scope uid is not 0.
MALDOCA_ASSERT_OK_AND_ASSIGN(
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(kSource, parse_request,
JsAstRepr repr, ToJsAstRepr::FromJsSourceRepr(source_repr, parse_request,
absl::InfiniteDuration(),
std::nullopt, babel));

Expand Down
3 changes: 3 additions & 0 deletions maldoca/js/babel/babel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct BabelGenerateResult {
// TODO: Determine if source_code and error can co-exist, or they should be in
// a std::variant.
std::optional<BabelError> error;

// The generated source map, if requested.
std::optional<std::string> source_map;
};

class Babel {
Expand Down
3 changes: 3 additions & 0 deletions maldoca/js/babel/babel.proto
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ message BabelGenerateOptions {

// Whether to minify the generated source.
optional bool compact = 2 [default = false];

// Whether to generate source maps.
optional bool source_maps = 3 [default = false];
}

message PositionPb {
Expand Down
1 change: 1 addition & 0 deletions maldoca/js/babel/babel_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ nlohmann::json BabelGenerateOptionsToJson(const BabelGenerateOptions &options,
{"comments", options.include_comments()},
{"compact", options.compact()},
{"base64DecodeStringLiterals", string_literals_base64_encoded},
{"sourceMaps", options.source_maps()},
};

return json;
Expand Down
5 changes: 5 additions & 0 deletions maldoca/js/babel/babel_internal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ message BabelGenerateRequest {

// Whether to minify the generated source.
optional bool compact = 3 [default = false];

// Whether to generate source maps.
optional bool source_maps = 4 [default = false];
}

message BabelResponse {
Expand All @@ -62,4 +65,6 @@ message BabelParseResponse {
message BabelGenerateResponse {
// Babel throws exceptions if the provided AST is invalid.
optional BabelError error = 1;

optional string source_map = 2;
}
23 changes: 23 additions & 0 deletions maldoca/js/babel/babel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,29 @@ TEST_P(BabelTest, GenerateCompact) {
}
}

TEST_P(BabelTest, GenerateSourceMap) {
static const char kSourceMap[] =
R"({"version":3,"names":["console","log"],"sources":["source.js"],"sourcesContent":[null],"mappings":"AAAAA,OAAO,CAACC,GAAG,CAAC,eAAe,CAAC","ignoreList":[]})";

std::unique_ptr<Babel> babel = GetParam().babel_factory();
BabelParseRequest request;
MALDOCA_ASSERT_OK_AND_ASSIGN(
BabelParseResult parse_result,
babel->Parse(kSource, request, absl::InfiniteDuration()));

BabelGenerateOptions options;
options.set_source_maps(true);

MALDOCA_ASSERT_OK_AND_ASSIGN(BabelGenerateResult generate_result,
babel->Generate(parse_result.ast_string, options,
absl::InfiniteDuration()));

EXPECT_EQ(generate_result.source_code, kSource);
EXPECT_EQ(generate_result.error, std::nullopt);
EXPECT_TRUE(generate_result.source_map.has_value());
EXPECT_THAT(*generate_result.source_map, StrEq(kSourceMap));
}

GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BabelTest);

} // namespace maldoca
88 changes: 47 additions & 41 deletions maldoca/js/driver/conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,78 +42,83 @@ namespace maldoca {
// -----------------------------------------------------------------------------

absl::StatusOr<JsAstStringRepr> ToJsAstStringRepr::FromJsSourceRepr(
absl::string_view source, BabelParseRequest parse_request,
const JsSourceRepr &source_repr, BabelParseRequest parse_request,
absl::Duration timeout, Babel &babel) {
MALDOCA_ASSIGN_OR_RETURN(BabelParseResult parse_result,
babel.Parse(source, parse_request, timeout));
return JsAstStringRepr{std::move(parse_result.ast_string)};
MALDOCA_ASSIGN_OR_RETURN(
BabelParseResult parse_result,
babel.Parse(source_repr.source, parse_request, timeout));
return JsAstStringRepr{std::move(parse_result.ast_string),
source_repr.source_map};
}

// -----------------------------------------------------------------------------
// AST string -> AST
// -----------------------------------------------------------------------------

absl::StatusOr<JsAstRepr> ToJsAstRepr::FromJsAstStringRepr(
const BabelAstString &ast_string,
const JsAstStringRepr &ast_string_repr,
std::optional<int> recursion_depth_limit) {
MALDOCA_ASSIGN_OR_RETURN(
std::unique_ptr<JsFile> ast,
GetFileAstFromAstString(ast_string, recursion_depth_limit));
return JsAstRepr{std::move(ast), ast_string.scopes()};
MALDOCA_ASSIGN_OR_RETURN(std::unique_ptr<JsFile> ast,
GetFileAstFromAstString(ast_string_repr.ast_string,
recursion_depth_limit));
return JsAstRepr{std::move(ast), ast_string_repr.ast_string.scopes(),
ast_string_repr.source_map};
}

// -----------------------------------------------------------------------------
// AST -> HIR
// -----------------------------------------------------------------------------

absl::StatusOr<JsHirRepr> ToJsHirRepr::FromJsAstRepr(
const JsFile &ast, const BabelScopes &scopes,
const JsAstRepr &ast_repr,
mlir::MLIRContext &mlir_context) {
MALDOCA_ASSIGN_OR_RETURN(mlir::OwningOpRef<JsirFileOp> op,
AstToJshirFile(ast, mlir_context));
return JsHirRepr{std::move(op), scopes};
AstToJshirFile(*ast_repr.ast, mlir_context));
return JsHirRepr{std::move(op), ast_repr.scopes, ast_repr.source_map};
}

// -----------------------------------------------------------------------------
// Source -> AST string -> AST
// -----------------------------------------------------------------------------

absl::StatusOr<JsAstRepr> ToJsAstRepr::FromJsSourceRepr(
absl::string_view source, BabelParseRequest parse_request,
const JsSourceRepr &source_repr, BabelParseRequest parse_request,
absl::Duration timeout, std::optional<int> recursion_depth_limit,
Babel &babel) {
MALDOCA_ASSIGN_OR_RETURN(JsAstStringRepr ast_string,
ToJsAstStringRepr::FromJsSourceRepr(
source, parse_request, timeout, babel));
return ToJsAstRepr::FromJsAstStringRepr(ast_string.ast_string,
recursion_depth_limit);
MALDOCA_ASSIGN_OR_RETURN(
JsAstStringRepr ast_string,
ToJsAstStringRepr::FromJsSourceRepr(source_repr,
parse_request, timeout, babel));
return ToJsAstRepr::FromJsAstStringRepr(ast_string, recursion_depth_limit);
}

// -----------------------------------------------------------------------------
// Source -> AST string -> AST -> HIR
// -----------------------------------------------------------------------------

absl::StatusOr<JsHirRepr> ToJsHirRepr::FromJsSourceRepr(
absl::string_view source, BabelParseRequest parse_request,
const JsSourceRepr &source_repr, BabelParseRequest parse_request,
absl::Duration timeout, std::optional<int> recursion_depth_limit,
Babel &babel, mlir::MLIRContext &mlir_context) {
MALDOCA_ASSIGN_OR_RETURN(JsAstRepr ast, ToJsAstRepr::FromJsSourceRepr(
source, parse_request, timeout,
recursion_depth_limit, babel));
return ToJsHirRepr::FromJsAstRepr(*ast.ast, ast.scopes, mlir_context);
MALDOCA_ASSIGN_OR_RETURN(
JsAstRepr ast,
ToJsAstRepr::FromJsSourceRepr(source_repr, parse_request, timeout,
recursion_depth_limit, babel));
return ToJsHirRepr::FromJsAstRepr(ast, mlir_context);
}

// -----------------------------------------------------------------------------
// AST string -> AST -> HIR
// -----------------------------------------------------------------------------

absl::StatusOr<JsHirRepr> ToJsHirRepr::FromJsAstStringRepr(
const BabelAstString &ast_string, std::optional<int> recursion_depth_limit,
const JsAstStringRepr &ast_string_repr,
std::optional<int> recursion_depth_limit,
mlir::MLIRContext &mlir_context) {
MALDOCA_ASSIGN_OR_RETURN(
JsAstRepr ast,
ToJsAstRepr::FromJsAstStringRepr(ast_string, recursion_depth_limit));
return ToJsHirRepr::FromJsAstRepr(*ast.ast, ast.scopes, mlir_context);
ToJsAstRepr::FromJsAstStringRepr(ast_string_repr, recursion_depth_limit));
return ToJsHirRepr::FromJsAstRepr(ast, mlir_context);
}

// =============================================================================
Expand All @@ -128,31 +133,33 @@ absl::StatusOr<JsAstRepr> ToJsAstRepr::FromJsHirRepr(
const JsHirRepr &hir_repr) {
MALDOCA_ASSIGN_OR_RETURN(std::unique_ptr<JsFile> ast,
JshirFileToAst(hir_repr.op.get()));
return JsAstRepr{std::move(ast), hir_repr.scopes};
return JsAstRepr{std::move(ast), hir_repr.scopes, hir_repr.source_map};
}

// -----------------------------------------------------------------------------
// AST -> AST string
// -----------------------------------------------------------------------------

absl::StatusOr<JsAstStringRepr> ToJsAstStringRepr::FromJsAstRepr(
const JsFile &ast, const BabelScopes &scopes) {
BabelAstString ast_string = GetAstStringFromFileAst(ast);
*ast_string.mutable_scopes() = scopes;
return JsAstStringRepr{std::move(ast_string)};
const JsAstRepr &ast_repr) {
BabelAstString ast_string = GetAstStringFromFileAst(*ast_repr.ast);
*ast_string.mutable_scopes() = ast_repr.scopes;
return JsAstStringRepr{std::move(ast_string), ast_repr.source_map};
}

// -----------------------------------------------------------------------------
// AST string -> Source
// -----------------------------------------------------------------------------

absl::StatusOr<JsSourceRepr> ToJsSourceRepr::FromJsAstStringRepr(
const BabelAstString &ast_string, BabelGenerateOptions generate_options,
absl::Duration timeout, Babel &babel) {
const JsAstStringRepr &ast_string_repr,
BabelGenerateOptions generate_options, absl::Duration timeout,
Babel &babel) {
MALDOCA_ASSIGN_OR_RETURN(
BabelGenerateResult generate_result,
babel.Generate(ast_string, generate_options, timeout));
return JsSourceRepr{std::move(generate_result.source_code)};
babel.Generate(ast_string_repr.ast_string, generate_options, timeout));
return JsSourceRepr{std::move(generate_result.source_code),
generate_result.source_map};
}

// -----------------------------------------------------------------------------
Expand All @@ -162,7 +169,7 @@ absl::StatusOr<JsSourceRepr> ToJsSourceRepr::FromJsAstStringRepr(
absl::StatusOr<JsAstStringRepr> ToJsAstStringRepr::FromJsHirRepr(
const JsHirRepr &hir_repr) {
MALDOCA_ASSIGN_OR_RETURN(JsAstRepr ast, ToJsAstRepr::FromJsHirRepr(hir_repr));
return ToJsAstStringRepr::FromJsAstRepr(*ast.ast, ast.scopes);
return ToJsAstStringRepr::FromJsAstRepr(ast);
}

// -----------------------------------------------------------------------------
Expand All @@ -174,7 +181,7 @@ absl::StatusOr<JsSourceRepr> ToJsSourceRepr::FromJsHirRepr(
absl::Duration timeout, Babel &babel) {
MALDOCA_ASSIGN_OR_RETURN(JsAstStringRepr ast_string,
ToJsAstStringRepr::FromJsHirRepr(hir_repr));
return ToJsSourceRepr::FromJsAstStringRepr(ast_string.ast_string,
return ToJsSourceRepr::FromJsAstStringRepr(ast_string,
generate_options, timeout, babel);
}

Expand All @@ -183,12 +190,11 @@ absl::StatusOr<JsSourceRepr> ToJsSourceRepr::FromJsHirRepr(
// -----------------------------------------------------------------------------

absl::StatusOr<JsSourceRepr> ToJsSourceRepr::FromJsAstRepr(
const JsFile &ast, BabelGenerateOptions generate_options,
const JsAstRepr &ast_repr, BabelGenerateOptions generate_options,
absl::Duration timeout, Babel &babel) {
BabelScopes dummy_scopes;
MALDOCA_ASSIGN_OR_RETURN(JsAstStringRepr ast_string,
ToJsAstStringRepr::FromJsAstRepr(ast, dummy_scopes));
return ToJsSourceRepr::FromJsAstStringRepr(ast_string.ast_string,
ToJsAstStringRepr::FromJsAstRepr(ast_repr));
return ToJsSourceRepr::FromJsAstStringRepr(ast_string,
generate_options, timeout, babel);
}

Expand Down
Loading
Loading