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

Fix String#chars #522

Merged
merged 3 commits into from
Dec 14, 2023
Merged
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
4 changes: 4 additions & 0 deletions builtin/string.sk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class String
ret
end

# Returns list of characters (more specifically,
# grapheme clusters).
#def chars -> Array<String>

# Call `f` for each byte
def each_byte(f: Fn1<Int, Void>)
@bytesize.times do |i|
Expand Down
18 changes: 13 additions & 5 deletions lib/skc_rustlib/src/builtin/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use crate::builtin::{SkInt, SkObj};
use crate::sk_methods::meta_array_new;
use shiika_ffi_macro::shiika_method;
use crate::builtin::{SkClass, SkInt, SkObj};
use crate::sk_cls::SkCls;
use shiika_ffi_macro::{shiika_const_ref, shiika_method, shiika_method_ref};

shiika_const_ref!("::Array", SkClass, "sk_Array");
shiika_method_ref!(
"Meta:Array#new",
fn(receiver: SkClass) -> SkAry<SkObj>,
"meta_array_new"
);

#[repr(C)]
#[derive(Debug)]
Expand All @@ -16,8 +23,9 @@ struct ShiikaArray<T> {

impl<T> SkAry<T> {
/// Call `Array.new`.
pub fn new<U>() -> SkAry<U> {
let sk_ary = meta_array_new(std::ptr::null());
pub fn new<U: SkCls>() -> SkAry<U> {
let spe_cls = sk_Array().specialize(vec![U::get_class_object()]);
let sk_ary = meta_array_new(spe_cls);
// Force cast because external function (Meta_Array_new)
// cannot have type a parameter.
SkAry(sk_ary.0 as *mut ShiikaArray<U>)
Expand Down
14 changes: 12 additions & 2 deletions lib/skc_rustlib/src/builtin/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
mod witness_table;
use crate::builtin::class::witness_table::WitnessTable;
use crate::builtin::{SkAry, SkInt, SkStr};
use crate::sk_methods::meta_class_new;
use shiika_ffi_macro::shiika_method;
use shiika_ffi_macro::{shiika_method, shiika_method_ref};
use std::collections::HashMap;

shiika_method_ref!(
"Meta:Class#new",
fn(receiver: *const u8) -> SkClass,
"meta_class_new"
);

#[repr(C)]
#[derive(Debug)]
pub struct SkClass(*mut ShiikaClass);
Expand All @@ -31,6 +37,10 @@ impl SkClass {
unsafe { &(*self.0).name }
}

pub fn specialize(self, tyargs: Vec<SkClass>) -> SkClass {
class_specialize(self, tyargs)
}

fn specialized_classes(&mut self) -> &mut HashMap<String, *mut ShiikaClass> {
unsafe { (*self.0).specialized_classes.as_mut().unwrap() }
}
Expand Down
12 changes: 10 additions & 2 deletions lib/skc_rustlib/src/builtin/string.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
//! Instance of `::String`
use crate::builtin::object::ShiikaObject;
use crate::builtin::{SkAry, SkInt, SkObj, SkPtr};
use shiika_ffi_macro::shiika_method;
use crate::builtin::{SkAry, SkClass, SkInt, SkObj, SkPtr};
use crate::sk_cls::SkCls;
use shiika_ffi_macro::{shiika_const_ref, shiika_method};
use std::ffi::CString;
use unicode_segmentation::UnicodeSegmentation;

shiika_const_ref!("::String", SkClass, "sk_String");
impl SkCls for SkStr {
fn get_class_object() -> SkClass {
sk_String()
}
}

extern "C" {
// TODO: better name
fn gen_literal_string(p: *const u8, bytesize: i64) -> SkStr;
Expand Down
1 change: 1 addition & 0 deletions lib/skc_rustlib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod allocator;
mod builtin;
mod sk_cls;
mod sk_methods;
5 changes: 5 additions & 0 deletions lib/skc_rustlib/src/sk_cls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::builtin::SkClass;

pub trait SkCls {
fn get_class_object() -> SkClass;
}
Loading