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

Documentation: Stealify Lang C++ Binding internals. #30

Open
lemanschik opened this issue Dec 20, 2022 · 1 comment
Open

Documentation: Stealify Lang C++ Binding internals. #30

lemanschik opened this issue Dec 20, 2022 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request Marketing

Comments

@lemanschik
Copy link
Owner

lemanschik commented Dec 20, 2022

The below shows how to create memory save modules via c++ bindings directly into our engine. This is How we make rust obsolete. And C++ Memory Save as it runs in our VM with the Memory it gets assigned to which again allows you and your company to reuse your existing C++ Codebases and still be compliant to the US Advice to use Memory Safe Languages over c++.

Stealify Lang is Also Fundamental Memory Safe as it Builds Every Module in its own Virtual Machine and then links the memory parts via handlers that are needed the security concepts and garanties of capability based protocols. It Implements by design a declarative in memory wire layout that is readable and understandable composed out of simple modules written in any language.

Example blake 3 Module with bindings.

The below is a hand written raw module example only to illustrate the C++ Template Function pattern
and to show a reusable example how to apply it to common c++ module like blake3 to create directly built In Engine bindings for it.

blake3.cc

#include "blake3.h"

void NAMESPACE::blake3::HashInit(const FunctionCallbackInfo<Value> &args) {
  Local<ArrayBuffer> ab = args[0].As<ArrayBuffer>();
  std::shared_ptr<BackingStore> buf = ab->GetBackingStore();
  blake3_hasher* hasher = (blake3_hasher*)calloc(1, sizeof(blake3_hasher));
  blake3_hasher_init(hasher);
  ab->SetAlignedPointerInInternalField(1, hasher);
}

void NAMESPACE::blake3::HashInitKeyed(const FunctionCallbackInfo<Value> &args) {
  Isolate *isolate = args.GetIsolate();
  Local<ArrayBuffer> ab = args[0].As<ArrayBuffer>();
  std::shared_ptr<BackingStore> buf = ab->GetBackingStore();
  String::Utf8Value str(isolate, args[1]);
  blake3_hasher* hasher = (blake3_hasher*)calloc(1, sizeof(blake3_hasher));
  const uint8_t* key = (const uint8_t*)*str;
  blake3_hasher_init_keyed(hasher, key);
  ab->SetAlignedPointerInInternalField(1, hasher);
}

void NAMESPACE::blake3::HashUpdate(const FunctionCallbackInfo<Value> &args) {
  Isolate *isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();
  Local<ArrayBuffer> outab = args[0].As<ArrayBuffer>();
  std::shared_ptr<BackingStore> out = outab->GetBackingStore();
  Local<ArrayBuffer> inab = args[1].As<ArrayBuffer>();
  std::shared_ptr<BackingStore> in = inab->GetBackingStore();
  blake3_hasher* hasher = (blake3_hasher*)outab->GetAlignedPointerFromInternalField(1);
  unsigned int off = args[2]->Uint32Value(context).ToChecked();
  unsigned int len = args[3]->Uint32Value(context).ToChecked();
  unsigned char* source = (unsigned char*)in->Data() + off;
  blake3_hasher_update(hasher, source, len);
}

void NAMESPACE::blake3::HashFinalize(const FunctionCallbackInfo<Value> &args) {
  Local<ArrayBuffer> outab = args[0].As<ArrayBuffer>();
  std::shared_ptr<BackingStore> out = outab->GetBackingStore();
  blake3_hasher* hasher = (blake3_hasher*)outab->GetAlignedPointerFromInternalField(1);
  blake3_hasher_finalize(hasher, (uint8_t*)out->Data(), BLAKE3_OUT_LEN);
}

void NAMESPACE::blake3::Init(Isolate* isolate, Local<ObjectTemplate> target) {
  Local<ObjectTemplate> module = ObjectTemplate::New(isolate);
  SET_METHOD(isolate, module, "create", HashInit);
  SET_METHOD(isolate, module, "createKeyed", HashInitKeyed);
  SET_METHOD(isolate, module, "update", HashUpdate);
  SET_METHOD(isolate, module, "finish", HashFinalize);

  SET_VALUE(isolate, module, "BLAKE3_KEY_LEN", Integer::New(isolate, BLAKE3_KEY_LEN));
  SET_VALUE(isolate, module, "BLAKE3_MAX_DEPTH", Integer::New(isolate, BLAKE3_MAX_DEPTH));
  SET_VALUE(isolate, module, "BLAKE3_OUT_LEN", Integer::New(isolate, BLAKE3_OUT_LEN));
  SET_VALUE(isolate, module, "BLAKE3_BLOCK_LEN", Integer::New(isolate, BLAKE3_BLOCK_LEN));

  SET_MODULE(isolate, target, "blake3", module);
}

blake3.h

#ifndef NAMESPACE_ZLIB_H
#define NAMESPACE_ZLIB_H

#include "stealify.h"
#include <blake3.h>

namespace NAMESPACE {

namespace blake3 {

void HashInit(const FunctionCallbackInfo<Value> &args);
void HashInitKeyed(const FunctionCallbackInfo<Value> &args);
void HashUpdate(const FunctionCallbackInfo<Value> &args);
void HashFinalize(const FunctionCallbackInfo<Value> &args);
void Init(Isolate* isolate, Local<ObjectTemplate> target);

}

}

extern "C" {
	void* _register_blake3() {
		return (void*)NAMESPACE::blake3::Init;
	}
}

#endif

What does the above give us is relative clear for a c++ 20 programmer. It will link properties and functions into a context that it gets as parameter. That will produce a context with that builtIn Functions that is later link able into our VM directly via creating a new context out of that which we can use like it would be nativ build in while this is fully Modular Load Able by the engine.

This pattern is n times faster then ffi or other methods it is modular and still as fast and sometimes even faster as a nativ build in module or component.

for the geeks this is what the compiler will do on the low level as Makefile syntax to be more understandable

export const blake3 = `$(CC) -g -s -shared -flto -pthread -m64 -Wl,--start-group blake3_lib.o blake3.o blake3_dispatch.o blake3_portable.o blake3_avx2_x86-64_unix.o blake3_avx512_x86-64_unix.o blake3_sse41_x86-64_unix.o -Wl,--end-group -Wl,-soname=${MODULE}.so -o ${MODULE}.so
	objcopy --only-keep-debug ${MODULE}.so ${MODULE}.so.debug
	strip --strip-debug --strip-unneeded ${MODULE}.so`
@lemanschik lemanschik self-assigned this Dec 20, 2022
@lemanschik lemanschik added documentation Improvements or additions to documentation enhancement New feature or request Marketing labels Dec 20, 2022
@lemanschik
Copy link
Owner Author

Stealify Lang Implements also VFS Interfaces to the lowest Level Of the Kernel Interfaces see: VFS minimal-boot

remember: vfs is not a userland thing this is the kernel vfs that implements read, open, write, close the kernel syscalls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request Marketing
Projects
None yet
Development

No branches or pull requests

1 participant