Skip to content

rust-hl-modules fails with file operations? #15

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

Closed
robitex opened this issue Aug 5, 2014 · 7 comments
Closed

rust-hl-modules fails with file operations? #15

robitex opened this issue Aug 5, 2014 · 7 comments
Labels

Comments

@robitex
Copy link

robitex commented Aug 5, 2014

Hi,
I'm starting to experiment your fantastic library, especially to create lib for Lua.
One uses the rust-image lib: it's ok if the code don't access to the file system otherwise the process crashed (with the same error reported below).
Another experiment is an attempt with rust-tabular... but fails.
So I began to think that I/O is the problem and I write this example: saving a text file from Lua perfectly functional in "only Rust" code.

#![feature(phase)]

#[phase(plugin)]
extern crate lua_mod = "rust-hl-lua-modules";

#[export_lua_module]
pub mod save {// <-- must be the name of the Lua module
    use std::io::File;
    fn savefile() {
        let p = Path::new("a-file.txt");
        let mut file = match File::create(&p) {
            Ok(f) => f,
            Err(e) => fail!("file error: {}", e),
        };
        file.write(b"content");
    }

    #[lua_module_init]
    fn init() {
        println!("module initialized!")
    }
}

Then I compile it with Cargo with this config:

[package]

name = "save"
version = "0.1.0"
authors = [ "my.username@my.email.com" ]

[[lib]]

name = "save" # the name of the executable to generate
crate_type = ["dylib"]

[dependencies.rust-hl-lua-modules]

git = "https://github.com/tomaka/rust-hl-lua"

In a terminal session the behaviour of the library is this:

roberto@roberto-desktop:~/Scrivania/test-save/test$ lua
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> s = require "save"
module initialized!
> s.savefile()

You've met with a terrible fate, haven't you?

fatal runtime error: Could not unwind stack, error = 5
Istruzione non consentita (core dump creato)

Can you help me?
Is a question of local library installed on my system?
Thank you very much.
My dev platform is an Ubuntu 14.04 with ultimate version of Rust (0.12.0-pre-nightly 25741603f), Cargo and github library.

@tomaka
Copy link
Owner

tomaka commented Aug 6, 2014

Thanks for reporting this.

This is because of the way the rust I/O works.
There are two issues here:

  • I/O functions don't work because the runtime is not initialized
  • fail! will leave the Rust function while unwinding and will crash

@tomaka tomaka added the bug label Aug 6, 2014
@tomaka
Copy link
Owner

tomaka commented Aug 6, 2014

Can't fix properly before rust-lang/rust#1793 is fixed

Using libc should work however.

tomaka added a commit that referenced this issue Aug 6, 2014
@tomaka
Copy link
Owner

tomaka commented Aug 6, 2014

Partially fixed. All of the Rust std should be usable now.

However:

  • A failure will likely trigger a crash instead of a Lua error (but should this really be fixed? if fail! crashes the program it's for a good reason)
  • You can't use return something from within an exported function (I didn't think about this while writing the fix, I'll have to rewrite some parts of the implementation)
  • I'm not a 100% sure that this is error-proof because of the Rust issue linked above.
  • This fix may make things a bit slower, but it's probably almost not measurable.

@robitex
Copy link
Author

robitex commented Aug 6, 2014

I can confirm that now the test program works but the function savefile() appears blocked after the file creation. In another test I obtain this error at compile time:

error: mismatched types: expected `()` but found `int`

due to what you say at the second point in the precedent list.
Thank you again.
R.

@tomaka
Copy link
Owner

tomaka commented Dec 19, 2014

Fixed thanks to rust-lang/rust#19654

@tomaka tomaka closed this as completed Dec 19, 2014
@robitex
Copy link
Author

robitex commented Dec 19, 2014

Uh. Great. Great job!

@robitex
Copy link
Author

robitex commented Dec 19, 2014

Hi,
after adding some semicolon on macro definition and macro call in "rust_table.rs", "functions_write.rs", "tuples.rs" and "values.rs", and modified the crate declaration in mylib.rs example program (no more equals sign), I receive an error on #[export_lua_module]:

src/mylib.rs:7:1: 7:21 error: obsolete syntax: `proc` expression
src/mylib.rs:7 #[export_lua_module]
               ^~~~~~~~~~~~~~~~~~~~
note: use a `move ||` expression instead
error: aborting due to previous error
Could not compile `mylib`.

This is the code of mylib.rs:

#![crate_type = "dylib"]
#![feature(phase)]

#[phase(plugin)]
extern crate "rust-hl-lua-modules" as lua;

#[export_lua_module]
pub mod mylib {// <-- must be the name of the Lua module
    // factorial function
    fn fact(n: int) -> int {
        let mut f = 1;
        for i in range(1, n+1) {
            f *= i;
        }
        return f;
    }

    #[lua_module_init]
    fn init() {
        println!("Ok. Module mylib initialized!")
    }
}

I think this is due to something to change in "rust-hl-lua-modules.rs" after the recent Rust breaking-change update but I'm not so expert to understand your code, too advanced for my knowledge...
I wish to help you... maybe with the documentation.
Thanks a lot.
R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants