git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Change FFI loading scheme
commit434ac1ae76e84576c8bbbbc579ccd53a2b5f2a7b authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 15 Jul 2019, 23:10 (7 years ago) parentc31d95b
3 files changed+20−6
runtime/purescript.go+13−2 src/CodeGen/IL.hs+1−1 src/CodeGen/IL/Printer.hs+6−3
runtime/purescript.go+13−2
@@ -26,6 +26,17 @@ type Fn10 = func(Any, Any, Any, Any, Any, Any, Any, Any, Any, Any) Any
2626
2727const Undefined = "undefined"
2828
29+var foreign = make(map[string]Map)
30+
31+func Foreign(key string) Map {
32+ value, found := foreign[key]
33+ if !found {
34+ value = make(Map)
35+ foreign[key] = value
36+ }
37+ return value
38+}
39+
2940func Apply(f Any, args ...Any) Any {
3041 result := f
3142 for _, arg := range args {
@@ -40,10 +51,10 @@ func Run(f Any, args ...Any) Any {
4051 return fn()
4152}
4253
43−func SafeGet(dict map[string]Any, key string) Any {
54+func Get(dict map[string]Any, key string) Any {
4455 value, ok := dict[key]
4556 if !ok {
46− panic("Foreign value '" + key + "' not defined")
57+ panic("Foreign value '" + key + "' not found")
4758 }
4859 return value
4960}
src/CodeGen/IL.hs+1−1
@@ -63,7 +63,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
6363 interface = interfaceSource modName values foreigns
6464 imports = nub . concat $ importToIL <$> optimized'
6565 implHeader = implHeaderSource modName imports ""
66− implFooter = implFooterSource modName foreigns
66+ implFooter = implFooterSource (runModuleName mn) foreigns
6767 return $ (interface, foreigns', optimized', implHeader, implFooter)
6868 where
6969 modName = moduleNameToIL mn
src/CodeGen/IL/Printer.hs+6−3
@@ -456,7 +456,7 @@ implFooterSource mn foreigns =
456456 (if null foreigns
457457 then ""
458458 else ("// Foreign values\n\n" <>
459− "var " <> foreignDict <> " = " <> dictType <> "{}\n\n" <>
459+ "var " <> foreignDict <> " = "<> foreignMod <> "(\"" <> mn <> "\")\n\n" <>
460460 (T.concat $ (\foreign' ->
461461 let name = moduleIdentToIL foreign' in
462462 varDecl <> " " <> initName name <> " Once\n" <>
@@ -466,7 +466,7 @@ implFooterSource mn foreigns =
466466 "() Any { \n" <>
467467 " " <> initName name <> ".Do(func() {\n" <>
468468 " " <> valueName name <> " = " <>
469− "SafeGet(" <> foreignDict <> ", " <>
469+ "Get(" <> foreignDict <> ", " <>
470470 (stringLiteral . mkString $ runIdent foreign') <> ")\n" <>
471471 " })\n" <>
472472 " return " <> valueName name <> "\n" <>
@@ -486,8 +486,11 @@ varDecl = "var"
486486renderArg :: Text -> Text -> Text
487487renderArg decl arg = arg <> " " <> decl
488488
489+foreignMod :: Text
490+foreignMod = "Foreign"
491+
489492foreignDict :: Text
490−foreignDict = "Foreign"
493+foreignDict = "foreign"
491494
492495initName :: Text -> Text
493496initName s = "ₒ" <> s