Unverified Commit 9be56aa6 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(next-swc): embed target triple string (#35416)



This PR straightly port over swc's target triple metadata (https://github.com/swc-project/swc/pull/4058) into next-swc, mainly for the tracking purpose of usages.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 73b83a0b
use std::{
env,
fs::File,
io::{BufWriter, Write},
path::Path,
};
extern crate napi_build; extern crate napi_build;
fn main() { fn main() {
let out_dir = env::var("OUT_DIR").expect("Outdir should exist");
let dest_path = Path::new(&out_dir).join("triple.txt");
let mut f =
BufWriter::new(File::create(&dest_path).expect("Failed to create target triple text"));
write!(
f,
"{}",
env::var("TARGET").expect("Target should be specified")
)
.expect("Failed to write target triple text");
napi_build::setup(); napi_build::setup();
} }
...@@ -71,6 +71,8 @@ fn init(mut exports: JsObject) -> napi::Result<()> { ...@@ -71,6 +71,8 @@ fn init(mut exports: JsObject) -> napi::Result<()> {
exports.create_named_method("parse", parse::parse)?; exports.create_named_method("parse", parse::parse)?;
exports.create_named_method("getTargetTriple", util::get_target_triple)?;
Ok(()) Ok(())
} }
......
...@@ -27,10 +27,17 @@ DEALINGS IN THE SOFTWARE. ...@@ -27,10 +27,17 @@ DEALINGS IN THE SOFTWARE.
*/ */
use anyhow::{Context, Error}; use anyhow::{Context, Error};
use napi::{CallContext, JsBuffer, Status}; use napi::{CallContext, Env, JsBuffer, JsString, Status};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::any::type_name; use std::any::type_name;
static TARGET_TRIPLE: &str = include_str!(concat!(env!("OUT_DIR"), "/triple.txt"));
#[contextless_function]
pub fn get_target_triple(env: Env) -> napi::ContextlessResult<JsString> {
env.create_string(TARGET_TRIPLE).map(Some)
}
pub trait MapErr<T>: Into<Result<T, anyhow::Error>> { pub trait MapErr<T>: Into<Result<T, anyhow::Error>> {
fn convert_err(self) -> napi::Result<T> { fn convert_err(self) -> napi::Result<T> {
self.into() self.into()
......
...@@ -233,3 +233,10 @@ export async function parse(src, options) { ...@@ -233,3 +233,10 @@ export async function parse(src, options) {
let parserOptions = getParserOptions(options) let parserOptions = getParserOptions(options)
return bindings.parse(src, parserOptions).then((astStr) => JSON.parse(astStr)) return bindings.parse(src, parserOptions).then((astStr) => JSON.parse(astStr))
} }
export function getBinaryMetadata() {
let bindings = loadBindingsSync()
return {
target: bindings.getTargetTriple(),
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment