forked from yorkie/rust.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.rs
62 lines (52 loc) · 2.52 KB
/
path.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::path::Path;
use std::path::MAIN_SEPARATOR;
use util::v8;
extern fn normalize(arguments: v8::FunctionCallbackInfo) {
let p = arguments.At(0).ToString().as_string();
let path = Path::new(&*p).to_str();
arguments.GetReturnValue().Set(v8::String::NewFromUtf8(path.unwrap()));
}
extern fn join(arguments: v8::FunctionCallbackInfo) {
// TODO
arguments.GetReturnValue().SetWithBool(true);
}
extern fn resolve(arguments: v8::FunctionCallbackInfo) {
// TODO
arguments.GetReturnValue().SetWithBool(true);
}
extern fn isAbsolute(arguments: v8::FunctionCallbackInfo) {
let p = arguments.At(0).ToString().as_string();
arguments.GetReturnValue().SetWithBool(Path::new(&*p).is_absolute());
}
extern fn relative(arguments: v8::FunctionCallbackInfo) {
// TODO
arguments.GetReturnValue().SetWithBool(true);
}
extern fn dirname(arguments: v8::FunctionCallbackInfo) {
let p = arguments.At(0).ToString().as_string();
let dirpath = Path::new(&*p).parent().unwrap().to_str();
arguments.GetReturnValue().Set(v8::String::NewFromUtf8(dirpath.unwrap()));
}
extern fn basename(arguments: v8::FunctionCallbackInfo) {
let p = arguments.At(0).ToString().as_string();
let filename = Path::new(&*p).file_name().unwrap().to_str();
arguments.GetReturnValue().Set(v8::String::NewFromUtf8(filename.unwrap()));
}
extern fn extname(arguments: v8::FunctionCallbackInfo) {
let p = arguments.At(0).ToString().as_string();
let extname = Path::new(&*p).extension().unwrap().to_str();
arguments.GetReturnValue().Set(v8::String::NewFromUtf8(extname.unwrap()));
}
pub fn Init() -> v8::Object {
let exports = v8::Object::New();
exports.Set(v8::String::NewFromUtf8("normalize"), v8::FunctionTemplate::New(normalize).GetFunction());
exports.Set(v8::String::NewFromUtf8("join"), v8::FunctionTemplate::New(join).GetFunction());
exports.Set(v8::String::NewFromUtf8("resolve"), v8::FunctionTemplate::New(resolve).GetFunction());
exports.Set(v8::String::NewFromUtf8("isAbsolute"), v8::FunctionTemplate::New(isAbsolute).GetFunction());
exports.Set(v8::String::NewFromUtf8("relative"), v8::FunctionTemplate::New(relative).GetFunction());
exports.Set(v8::String::NewFromUtf8("dirname"), v8::FunctionTemplate::New(dirname).GetFunction());
exports.Set(v8::String::NewFromUtf8("basename"), v8::FunctionTemplate::New(basename).GetFunction());
exports.Set(v8::String::NewFromUtf8("extname"), v8::FunctionTemplate::New(extname).GetFunction());
exports.Set(v8::String::NewFromUtf8("sep"), v8::String::NewFromUtf8(&*MAIN_SEPARATOR.to_string()));
return exports;
}