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

Basic support for dynamically loading syntax extensions #6735

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub fn compile_rest(sess: Session,

crate = time(time_passes, ~"expansion", ||
syntax::ext::expand::expand_crate(sess.parse_sess, copy cfg,
sess.opts.dynamic_syntax_extensions,
crate));

crate = time(time_passes, ~"configuration", ||
Expand Down Expand Up @@ -652,6 +653,7 @@ pub fn build_session_options(binary: @~str,
let debuginfo = debugging_opts & session::debug_info != 0 ||
extra_debuginfo;
let statik = debugging_opts & session::statik != 0;
let dynamic_syntax_extensions = debugging_opts & session::dynamic_syntax_extensions != 0;
let target =
match target_opt {
None => host_triple(),
Expand Down Expand Up @@ -700,7 +702,8 @@ pub fn build_session_options(binary: @~str,
parse_only: parse_only,
no_trans: no_trans,
debugging_opts: debugging_opts,
android_cross_path: android_cross_path
android_cross_path: android_cross_path,
dynamic_syntax_extensions: dynamic_syntax_extensions
};
return sopts;
}
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub static extra_debug_info: uint = 1 << 21;
pub static statik: uint = 1 << 22;
pub static print_link_args: uint = 1 << 23;
pub static no_debug_borrows: uint = 1 << 24;
pub static dynamic_syntax_extensions: uint = 1 << 25;

pub fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
~[(~"verbose", ~"in general, enable more debug printouts", verbose),
Expand Down Expand Up @@ -107,6 +108,9 @@ pub fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
(~"no-debug-borrows",
~"do not show where borrow checks fail",
no_debug_borrows),
(~"dynamic-syntax-extensions",
~"allow loading of syntax extensions via #[syntax_extension] (experimental)",
dynamic_syntax_extensions)
]
}

Expand Down Expand Up @@ -148,6 +152,8 @@ pub struct options {
no_trans: bool,
debugging_opts: uint,
android_cross_path: Option<~str>,
// whether syntax extensions can be loaded
dynamic_syntax_extensions: bool
}

pub struct crate_metadata {
Expand Down Expand Up @@ -318,6 +324,7 @@ pub fn basic_options() -> @options {
no_trans: false,
debugging_opts: 0u,
android_cross_path: None,
dynamic_syntax_extensions: false
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/astsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ fn build_ctxt(sess: Session,

let ast = config::strip_unconfigured_items(ast);
let ast = syntax::ext::expand::expand_crate(sess.parse_sess,
copy sess.opts.cfg, ast);
copy sess.opts.cfg,
false, // no dynamic syntax extensions
ast);
let ast = front::test::modify_for_testing(sess, ast);
let ast_map = ast_map::map_crate(sess.diagnostic(), ast);

Expand Down
193 changes: 193 additions & 0 deletions src/libstd/unstable/dynamic_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/*!

Dynamic libraries.

Highly experimental, no guarantees if it works with non-Rust
libraries, or anything other than libraries compiled with the exact
Rust compiler that the program using this was compiled with.

*/

use str;
use cast;
use option::{None, Option};
use result::{Ok, Err, Result};
use libc::{c_int, c_void};
use ops::Drop;
use ptr::Ptr;

mod raw {
use libc::{c_char, c_int, c_void};

#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
pub enum RTLD {
LAZY = 1,
NOW = 2,
GLOBAL = 256,
LOCAL = 0
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
pub extern {
fn dlopen(name: *c_char, mode: c_int) -> *c_void;
fn dlsym(handle: *c_void, name: *c_char) -> *c_void;
fn dlerror() -> *c_char;
fn dlclose(handle: *c_void);
}

#[cfg(target_os = "win32")]
pub extern {
fn LoadLibrary(name: *c_char);
fn GetProcAddress(handle: *c_void, name: *c_char) -> *c_void;
fn FreeLibrary(handle: *c_void);
}
}

/// An object representing a dynamic library
pub struct DynamicLib {
priv handle: *c_void
}

/// An object representing a symbol from a dynamic library. A
/// work-around for either extern fns becoming &extern fn (so that
/// they can have a lifetime), or #5922.
pub struct TaggedSymbol<'self, T> {
priv sym: T,
priv lifetime: Option<&'self ()>
}

impl DynamicLib {
/// Open a dynamic library.
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
pub fn open(name: &str) -> Result<DynamicLib, ~str> {
let handle = do str::as_c_str(name) |n| {
unsafe { raw::dlopen(n, raw::NOW as c_int | raw::GLOBAL as c_int) }
};
if handle.is_not_null() {
Ok(DynamicLib { handle: handle })
} else {
Err(
unsafe {
let error = raw::dlerror();
if error.is_not_null() {
str::raw::from_c_str(error)
} else {
~"unknown error"
}
})
}
}
/// Open a dynamic library.
#[cfg(target_os = "win32")]
pub fn open(name: &str) -> Result<DynamicLib, ~str> {
let handle = do str::as_c_str(name) |n| {
unsafe { raw::LoadLibrary(n) }
};
if handle.is_not_null() {
Ok(DynamicLib { handle: handle })
} else {
let err = unsafe { intrinsics::GetLastError() } as uint;
// XXX: make this message nice
Err(fmt!("`LoadLibrary` failed with code %u", err))
}
}

/// Retrieve a pointer to a symbol from the library. Note: this
/// operation has no way of knowing if the symbol actually has
/// type `T`, and so using the returned value can crash the
/// program, not just cause the task to fail.
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
pub fn get_symbol<'r, T>(&'r self, name: &str) -> Result<TaggedSymbol<'r, T>, ~str> {
let sym = do str::as_c_str(name) |n| {
unsafe { raw::dlsym(self.handle, n) }
};
if sym.is_not_null() {
Ok(TaggedSymbol {
sym: unsafe { cast::transmute(sym) },
lifetime: None::<&'r ()>
})
} else {
let error = unsafe { raw::dlerror() };
if error.is_not_null() {
Err(unsafe { str::raw::from_c_str(error) })
} else {
Err(~"unknown error")
}
}
}

/// Retrieve a pointer to a symbol from the library. Note: this
/// operation has no way of knowing if the symbol actually has
/// type `T`, and so using the returned value can crash the
/// program, not just cause the task to fail.
#[cfg(target_os = "win32")]
pub fn get_symbol<'r, T>(&'r self, name: &str) -> Result<TaggedSymbol<'r, T>, ~str> {
let sym = do str::as_c_str(name) |n| {
unsafe { raw::GetProcAddress(self.handle, n) }
};
if sym.is_not_null() {
Ok(TaggedSymbol { sym: unsafe { cast::transmute(sym) },
lifetime: None::<&'r ()> })
} else {
let err = unsafe { intrinsics::GetLastError() } as uint;
Err(fmt!("`GetProcAddress` failed with code %u", err))
}
}
}

#[unsafe_destructor]
impl Drop for DynamicLib {
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn finalize(&self) {
if self.handle.is_not_null() {
unsafe { raw::dlclose(self.handle) }
}
}

#[cfg(target_os = "win32")]
fn finalize(&self) {
if self.handle.is_not_null() {
unsafe { raw::FreeLibrary(self.handle) }
}
}
}

impl<'self, T> TaggedSymbol<'self, T> {
/// Get a reference to the (reference to the) symbol.
///
/// WARNING: Using the returned value can lead to crashes, since
/// the symbol may not really have type `T`.
///
/// WARNING: Storing the derefence of this pointer can lead to
/// crashes, if the library the symbol comes from has been dropped
/// before using the stored symbol (the compiler doesn't give any
/// indication that this is a problem).
pub unsafe fn get<'r>(&'r self) -> &'r T {
&'r self.sym
}
}
1 change: 1 addition & 0 deletions src/libstd/unstable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use comm::{GenericChan, GenericPort};
use prelude::*;
use task;

pub mod dynamic_lib;
pub mod at_exit;
pub mod global;
pub mod finally;
Expand Down
16 changes: 8 additions & 8 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ pub enum Transformer {
ScopeMacros(bool)
}

// utility function to simplify creating NormalTT syntax extensions
pub fn builtin_normal_tt(f: SyntaxExpanderTTFun) -> @Transformer {
@SE(NormalTT(SyntaxExpanderTT{expander: f, span: None}))
}
// utility function to simplify creating IdentTT syntax extensions
pub fn builtin_item_tt(f: SyntaxExpanderTTItemFun) -> @Transformer {
@SE(IdentTT(SyntaxExpanderTTItem{expander: f, span: None}))
}
// The base map of methods for expanding syntax extension
// AST nodes into full ASTs
pub fn syntax_expander_table() -> SyntaxEnv {
// utility function to simplify creating NormalTT syntax extensions
fn builtin_normal_tt(f: SyntaxExpanderTTFun) -> @Transformer {
@SE(NormalTT(SyntaxExpanderTT{expander: f, span: None}))
}
// utility function to simplify creating IdentTT syntax extensions
fn builtin_item_tt(f: SyntaxExpanderTTItemFun) -> @Transformer {
@SE(IdentTT(SyntaxExpanderTTItem{expander: f, span: None}))
}
let mut syntax_expanders = HashMap::new();
// NB identifier starts with space, and can't conflict with legal idents
syntax_expanders.insert(@~" block",
Expand Down
Loading