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

Usage as a library #21

Closed
ClementNerma opened this issue Jan 15, 2020 · 5 comments
Closed

Usage as a library #21

ClementNerma opened this issue Jan 15, 2020 · 5 comments

Comments

@ClementNerma
Copy link

Hi,

I'm currently developping a virtual machine library that uses custom instructions, which means a custom assembly language.

I've used CustomASM for a while now using the CLI and I really enjoy the possibility of the projects (alignment, labels, arithmetic, etc.), but I'd like to integrate it to my library. Sadly, when I looked at your files, it doesn't seem you expose enough functions to enable concrete usage as a library :/

Do you have any plan to enable such usage? It'd be really great to be able to do something like:

let src: &'static str = "<<< asm code here >>>";
let hex: Vec<u8> = customasm::assemble_to_hex(src).unwrap();

println!("Hex: {}", hex);

Thanks in advance for your answer :)

@hlorenzi
Copy link
Owner

There could absolutely have a more convenient function for this use-case, but as of now, if you take a look at the webasm interface, it shows a way you can do it. Then, you may use output.generate_binary(0, output.len()) to generate a byte vector. Let me know if you need more help!

@ClementNerma
Copy link
Author

I indeed saw this method, but sadly it's unsafe and I'm working in a crate that forbids any unsafe block :/
Is there any other way to use the library without unsafe?

@hlorenzi
Copy link
Owner

You can remove the unsafe parts; maybe something like the following? I haven't tested it, though:

fn assemble(src: &str) -> Vec<u8>
{
	let mut fileserver = FileServerMock::new();
	fileserver.add("asm", src.clone());
	
	let assemble = |report: RcReport, fileserver: &FileServerMock, filename: &str| -> Result<Vec<u8>, ()>
	{
		let mut asm = AssemblerState::new();
		asm.process_file(report.clone(), fileserver, filename)?;
		asm.wrapup(report.clone())?;
		
		let output = asm.get_binary_output();
		Ok(output.generate_binary(0, output.len()))
	};
		
	let report = RcReport::new();
	
	match assemble(report.clone(), &fileserver, "asm")
	{
		Ok(output) => output,
		Err(_) => panic!("error assembling")
	}
}

@hlorenzi
Copy link
Owner

I've added a new function, so you can now do:

customasm::assemble_str_to_binary("your string")

You can retrieve the byte array by doing:

let bytes = customasm::assemble_str_to_binary("your string").0.unwrap();

@ClementNerma
Copy link
Author

Sorry for the late answer I just received for notification.
Thanks a lot for the new function, it's really more simple to use now 👍

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

No branches or pull requests

2 participants