brenhinkeller / StaticTools.jl

Enabling StaticCompiler.jl-based compilation of (some) Julia code to standalone native binaries by avoiding GC allocations and llvmcall-ing all the things!
MIT License
167 stars 11 forks source link

Iteration Base API #41

Closed tshort closed 1 year ago

tshort commented 1 year ago

This changes the iteration to support the Base API to return Nothing as appropriate. The following example statically compiles:

using StaticCompiler
using StaticTools
using Libdl

function testfun1()
    if startswith(c"foobar", c"foo")
        return 1
    else
        return 0
    end
end

name = repr(testfun1)
filepath = compile_shlib(testfun1, (), "./", name)

# Open dylib
ptr = Libdl.dlopen(filepath, Libdl.RTLD_LOCAL)
fptr = Libdl.dlsym(ptr, "julia_$name")
ccall(fptr, Int, ())
brenhinkeller commented 1 year ago

Tests are passing so I'll merge and add an integration test -- I do have some concern that the whole "if startswith(...)" may be getting optimized out rather than compiled (which is fine here), but if so and this doesn't scale to cases where the content of the strings isn't statically known at compile-time then we can always add startswith/endswith (etc.) overloads that don't depend on Base.iterate