Skip to content

Commit

Permalink
✨ feat: Add TranspileOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 8, 2024
1 parent 7e0b18a commit 7f49688
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 40 deletions.
16 changes: 9 additions & 7 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@

use deno_ast::*;

pub const VERSION: &'static str = "0.1.0";
use crate::options;

pub fn transpile<'local>(code: String, media_type: MediaType, file_name: String) {
let url = ModuleSpecifier::parse(&format!("file:///{}", file_name)).unwrap();
const VERSION: &'static str = "0.1.0";

pub fn transpile<'local>(code: String, options: options::TranspileOptions) {
let url = ModuleSpecifier::parse(&format!("file:///{}", options.file_name)).unwrap();
println!("url: {}", url.to_string());
println!("source: {}", code.to_string());
println!("media_type: {}", media_type.to_string());
println!("media_type: {}", options.media_type.to_string());
let parsed_source = parse_module(ParseParams {
specifier: url.to_string(),
text_info: SourceTextInfo::from_string(code.to_string()),
media_type: media_type,
media_type: options.media_type,
capture_tokens: false,
maybe_syntax: None,
scope_analysis: false,
Expand All @@ -39,6 +41,6 @@ pub fn transpile<'local>(code: String, media_type: MediaType, file_name: String)
println!("{}", transpiled_js_code.text);
}

pub fn get_version<'local>() -> String {
VERSION.to_string()
pub fn get_version<'local>() -> &'local str {
VERSION
}
22 changes: 10 additions & 12 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,34 @@
* limitations under the License.
*/

use jni::objects::JClass;
use jni::sys::{jint, jobject, jstring};
use jni::JNIEnv;
use jni::objects::JClass;
use jni::sys::{jobject, jstring};
use options::FromJniType;

use std::ptr::null_mut;

mod core;
mod utils;

pub use core::VERSION;
pub mod core;
pub mod options;
pub mod utils;

#[no_mangle]
pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreGetVersion<'local>(
env: JNIEnv<'local>,
_: JClass<'local>,
) -> jstring {
utils::converter::string_to_jstring(&env, core::get_version().as_str())
utils::converter::string_to_jstring(&env, core::get_version())
}

#[no_mangle]
pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreTranspile<'local>(
mut env: JNIEnv<'local>,
_: JClass<'local>,
code: jstring,
media_type_id: jint,
file_name: jstring,
options: jobject,
) -> jobject {
let code = utils::converter::jstring_to_string(&mut env, code);
let file_name = utils::converter::jstring_to_string(&mut env, file_name);
let media_type = utils::converter::media_type_id_to_media_type(media_type_id);
core::transpile(code, media_type, file_name);
let options = options::TranspileOptions::from_jni_type(&mut env, options);
core::transpile(code, options);
null_mut()
}
76 changes: 76 additions & 0 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use jni::objects::JObject;
use jni::sys::jobject;
use jni::JNIEnv;

use deno_ast::MediaType;

use crate::utils;

const METHOD_TRANSPILE_OPTIONS_GET_FILE_NAME: &'static str = "getFileName";
const SIG_TRANSPILE_OPTIONS_GET_FILE_NAME: &'static str = "()Ljava/lang/String;";

const METHOD_TRANSPILE_OPTIONS_GET_MEDIA_TYPE: &'static str = "getMediaType";
const SIG_TRANSPILE_OPTIONS_GET_MEDIA_TYPE: &'static str = "()Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;";

const METHOD_MEDIA_TYPE_GET_ID: &'static str = "getId";
const SIG_MEDIA_TYPE_GET_ID: &'static str = "()I";

pub trait FromJniType {
fn from_jni_type<'local>(env: &mut JNIEnv<'local>, o: jobject) -> Self;
}

#[derive(Debug)]
pub struct TranspileOptions {
pub file_name: String,
pub media_type: MediaType,
}

impl FromJniType for TranspileOptions {
fn from_jni_type<'local>(env: &mut JNIEnv<'local>, o: jobject) -> TranspileOptions {
let o = unsafe { JObject::from_raw(o) };
// file_name
let file_name = env.call_method(
o.as_ref(),
METHOD_TRANSPILE_OPTIONS_GET_FILE_NAME,
SIG_TRANSPILE_OPTIONS_GET_FILE_NAME,
&[],
);
let file_name = unsafe { file_name.unwrap().as_jni().l };
let file_name = utils::converter::jstring_to_string(env, file_name);
// media_type
let media_type = env.call_method(
o.as_ref(),
METHOD_TRANSPILE_OPTIONS_GET_MEDIA_TYPE,
SIG_TRANSPILE_OPTIONS_GET_MEDIA_TYPE,
&[],
);
let media_type = unsafe { JObject::from_raw(media_type.unwrap().as_jni().l) };
let media_type = env.call_method(
media_type.as_ref(),
METHOD_MEDIA_TYPE_GET_ID,
SIG_MEDIA_TYPE_GET_ID,
&[],
);
let media_type = unsafe { media_type.unwrap().as_jni().i };
let media_type = utils::converter::media_type_id_to_media_type(media_type);
// construct
TranspileOptions { file_name, media_type }
}
}
6 changes: 3 additions & 3 deletions rust/tests/test_version.rs → rust/tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

use swc4j::*;
use swc4j::core::*;

#[test]
fn test_version() {
assert_eq!(VERSION, "0.1.0");
fn test_get_version() {
assert_eq!(get_version(), "0.1.0");
}
4 changes: 2 additions & 2 deletions src/main/java/com/caoccao/javet/swc4j/Swc4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import com.caoccao.javet.swc4j.options.Swc4jTranspileOptions;
import com.caoccao.javet.swc4j.outputs.Swc4jTranspileOutput;
import com.caoccao.javet.swc4j.utils.AssertionUtils;

/**
* The type Swc4j.
Expand Down Expand Up @@ -57,7 +58,6 @@ public Swc4jTranspileOutput transpile(String code) throws Swc4jCoreException {
public Swc4jTranspileOutput transpile(String code, Swc4jTranspileOptions options) throws Swc4jCoreException {
return (Swc4jTranspileOutput) Swc4jNative.coreTranspile(
code,
options.getMediaType().getId(),
options.getFileName());
AssertionUtils.notNull(options, "Options"));
}
}
5 changes: 1 addition & 4 deletions src/main/java/com/caoccao/javet/swc4j/Swc4jNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@ private Swc4jNative() {

static native String coreGetVersion();

static native Object coreTranspile(
String code,
int mediaTypeId,
String fileName);
static native Object coreTranspile(String code, Object options);
}
31 changes: 31 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/enums/Swc4jMediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package com.caoccao.javet.swc4j.enums;

import java.util.stream.Stream;

/**
* The enum Swc4j media type.
*
* @since 0.1.0
*/
public enum Swc4jMediaType {
JavaScript(0),
Jsx(1),
Expand All @@ -34,12 +41,36 @@ public enum Swc4jMediaType {
SourceMap(14),
Unknown(15);

private static final int LENGTH = 16;
private static final Swc4jMediaType[] TYPES = new Swc4jMediaType[LENGTH];

static {
Stream.of(values()).forEach(v -> TYPES[v.getId()] = v);
}

private final int id;

Swc4jMediaType(int id) {
this.id = id;
}

/**
* Parse swc4j media type.
*
* @param id the id
* @return the swc4j media type
* @since 0.1.0
*/
public static Swc4jMediaType parse(int id) {
return id >= 0 && id < LENGTH ? TYPES[id] : Unknown;
}

/**
* Gets id.
*
* @return the id
* @since 0.1.0
*/
public int getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package com.caoccao.javet.swc4j.options;

import com.caoccao.javet.swc4j.enums.Swc4jMediaType;
import com.caoccao.javet.swc4j.utils.AssertionUtils;

public final class Swc4jTranspileOptions {
public static final String DEFAULT_FILE_NAME = "main.js";
private String fileName;
private Swc4jMediaType mediaType;

public Swc4jTranspileOptions() {
setFileName(null);
setFileName(DEFAULT_FILE_NAME);
setMediaType(Swc4jMediaType.JavaScript);
}

Expand All @@ -36,12 +38,12 @@ public Swc4jMediaType getMediaType() {
}

public Swc4jTranspileOptions setFileName(String fileName) {
this.fileName = fileName;
this.fileName = AssertionUtils.notNull(fileName, "File name");
return this;
}

public Swc4jTranspileOptions setMediaType(Swc4jMediaType mediaType) {
this.mediaType = mediaType;
this.mediaType = AssertionUtils.notNull(mediaType, "Media type");
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@

public class Swc4jTranspileOutput {
protected String code;
protected String map;
protected boolean module;

public Swc4jTranspileOutput() {
this(null, null);
this(null);
}

public Swc4jTranspileOutput(String code) {
this(code, null);
this(code, false);
}

public Swc4jTranspileOutput(String code, String map) {
public Swc4jTranspileOutput(String code, boolean module) {
setCode(code);
setMap(map);
setModule(module);
}

public String getCode() {
return code;
}

public String getMap() {
return map;
public boolean isModule() {
return module;
}

public Swc4jTranspileOutput setCode(String code) {
this.code = code;
return this;
}

public Swc4jTranspileOutput setMap(String map) {
this.map = map;
public Swc4jTranspileOutput setModule(boolean module) {
this.module = module;
return this;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/utils/AssertionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.swc4j.utils;

public final class AssertionUtils {

private static final String VALUE = "Value";

private AssertionUtils() {
}

public static <T> T notNull(T value) {
return notNull(value, VALUE);
}

public static <T> T notNull(T value, String name) {
if (value == null) {
throw new NullPointerException(name + " is not nullable");
}
return value;
}
}

0 comments on commit 7f49688

Please sign in to comment.