Use native fn's link name attribute if given

Fixes issue #905
This commit is contained in:
Haitao Li 2011-11-15 22:30:03 -06:00
parent 0898d441cb
commit eae478cd74
2 changed files with 27 additions and 2 deletions

View File

@ -5805,9 +5805,15 @@ fn item_path(item: @ast::item) -> [str] { ret [item.ident]; }
fn collect_native_item(ccx: @crate_ctxt, i: @ast::native_item, &&pt: [str],
_v: vt<[str]>) {
alt i.node {
ast::native_item_fn(_, _, _) {
ast::native_item_fn(link_name, _, _) {
if !ccx.obj_methods.contains_key(i.id) {
register_native_fn(ccx, i.span, pt, i.ident, i.id);
let name =
if option::is_some(link_name) {
option::get(link_name)
} else {
i.ident
};
register_native_fn(ccx, i.span, pt, name, i.id);
}
}
_ { }

View File

@ -0,0 +1,19 @@
use std;
import std::vec;
import std::str;
native "cdecl" mod libc = "" {
fn my_strlen(str: *u8) -> uint = "strlen";
}
fn strlen(str: str) -> uint unsafe {
// C string is terminated with a zero
let bytes = str::bytes(str) + [0u8];
ret libc::my_strlen(vec::unsafe::to_ptr(bytes));
}
fn main(_args: [str]) {
let len = strlen("Rust");
assert(len == 4u);
}